douchuanchai2793 2019-08-04 17:32
浏览 58
已采纳

Wordpress - 首先查询自定义帖子类型的名称

I want to query first and last name using custom post type metaboxes.

Code i am using:

$name = 'John Doe';

$args = array( 
    'post_type'     => 'customers',
    'post_status'   => array( 'publish', 'pending', 'draft' ),
    'numberposts'   => -1,
    'order'         => 'ASC',
    'orderby'       => 'meta_value',
    'meta_key'      => 'customer_first_name'
    'meta_query'    => array(
        'relation'      => 'OR',
        array(
            'key'           => 'customer_first_name',
            'value'         => $name,
            'compare'       => 'LIKE',
        ),
        array(
            'key'           => 'customer_last_name',
            'value'         => $name,
            'compare'       => 'LIKE',
        ),      
    ),  
);  

$customers = get_posts($args);

$num = count( $customers );

If i search only "John" results are showing up. If i search full name "John Doe" there are no results. Why is that?

  • 写回答

1条回答 默认 最新

  • douxun4860 2019-08-04 17:40
    关注

    Of course it's show no result because you don't split the name first name and last name.

    $name = 'John Doe';
    $parts = explode(" ", $name);
    $first_name = $parts[0];
    $last_name = (count($name) > 1) ? $name[count($name)-1]: $first_name;
    $args = array( 
        'post_type'     => 'customers',
        'post_status'   => array( 'publish', 'pending', 'draft' ),
        'numberposts'   => -1,
        'order'         => 'ASC',
        'orderby'       => 'meta_value',
        'meta_key'      => 'customer_first_name',
        'meta_query'    => array(
            'relation'      => 'OR',
            array(
                'key'           => 'customer_first_name',
                'value'         => $first_name,
                'compare'       => 'LIKE',
            ),
            array(
                'key'           => 'customer_last_name',
                'value'         => $last_name,
                'compare'       => 'LIKE',
            ),      
        ),  
    );  
    
    $customers = get_posts($args);
    
    $num = count( $customers );
    

    Like international name, if last name is blank then last name will use first name too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题