网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

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

GG网络技术分享 2025-03-18 16:12 2


问题描述:

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?

网友观点:

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.

标签:

提交需求或反馈

Demand feedback