samedi 31 janvier 2015

Merge WP_Post and WP_User objects from two separate queries?


I am trying to set up a custom search functionality on a site I am working on. The search currently searches for posts/pages and users based on a set of custom defined user meta.


I have the standard query and the user query stored in variables, but I'm wondering if I can merge the WP_Post and WP_User objects into a single variable to loop over.


If not, how can I go about combining the two terms, so that they appear in the same search results.


This is what I currently have set up



global $wp_query;
$search_term = get_search_query();

$posts = $wp_query->posts;

$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'user_nicename',
'value' => sanitize_title( $search_term ),
'compare' => 'LIKE',
),
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE',
),
),
);
$user_meta_search = new WP_User_Query( $args );

$results = $user_meta_search->get_results();

if( !empty( $results ) ) {
$wp_query->posts = array_merge( $results , $wp_query->posts );
}


This currently doesn't loop properly because it's combining WP_Posts and WP_User objects.


Thanks





Aucun commentaire:

Enregistrer un commentaire