admin管理员组文章数量:1417555
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?
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?
Share Improve this question edited Aug 4, 2019 at 16:47 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Aug 4, 2019 at 15:56 JosephMurphyJosephMurphy 31 bronze badge 1 |1 Answer
Reset to default 0$name = 'John Doe';
// Split $name
$name = preg_split( '/\s+/', trim( $name ) );
// Set first name
$first_name = $name[0];
// Last name was provided?
$last_name = isset( $name[1] ) ? $name[1] : null;
// Define meta query args array
$meta_query_args = array();
// Set "first name" meta query or "first name" + "last name" if both exist
if ( is_null( $last_name ) ) {
$meta_query_args[] = array(
'key' => 'customer_first_name',
'value' => $first_name,
'compare' => 'LIKE',
);
} else {
$meta_query_args[] = array(
'relation' => 'AND', // This is default, just trying to be descriptive
array(
'key' => 'customer_first_name',
'value' => $first_name,
'compare' => 'LIKE'
),
array(
'key' => 'customer_last_name',
'value' => $last_name,
'compare' => 'LIKE'
)
);
}
// Set 'meta_query' in your $args variable
$args['meta_query'] = $meta_query_args;
Note that you'll have to add extra code to support "last name" + "first name" besides of "first name" + "last name" (order matters).
本文标签: searchQuery first and last name from custom post type
版权声明:本文标题:search - Query first and last name from custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745269877a2650823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
LIKE
comparison, which means that both the first and last names need to match `%John Doe%, which isn't going to work because neither field has both names. – Jacob Peattie Commented Aug 5, 2019 at 3:15