Wordpress - 从自定义帖子类型中首先查询和名称

我想使用自定义帖子类型元框查询名字和姓氏。


我正在使用的代码:


$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 );

如果我只搜索“John”,结果就会出现。如果我搜索全名“John Doe”,则没有结果。这是为什么?


慕仙森
浏览 131回答 1
1回答

慕斯王

当然,它不会显示任何结果,因为您没有拆分名字和姓氏。$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 );与国际名称一样,如果姓氏为空,则姓氏也将使用名字。
打开App,查看更多内容
随时随地看视频慕课网APP