John Davidson

php - WP QUERY to search WP Post and WP User Tables

0 comments
Message:


My Goal:


I am building a wordpress advance search where the search query is searched to all post (includes title and content) and in the Users Meta Table (nickname, firstname, lastname, displayname)


My Code


$search = $query->query_vars['s']
$term = explode(' ',$search);


$query_posts = $wpdb->prepare("SELECT * FROM {$wpdb->posts}, {$wpdb->usermeta} WHERE post_status='publish' AND ((post_title LIKE '%%%s%%') OR (post_content LIKE '%%%s%%') OR (nickname LIKE '%%%s%%') OR (display_name LIKE '%%%s%%')OR (first_name LIKE '%%%s%%') OR (last_name LIKE '%%%s%%') OR (nicename LIKE '%%%s%%'))", $term, $term)

But obviously, it's not working because i'm not sure if i'm doing the $query_post correctly. It's not returning search results.


The error


It's not really an error but the problem with the code above is its not returning the correct results. It's as if the code didn't look into the User Meta Table. I'm not sure if the SQL query is correct or anything.


Appreciate your help on this. Thanks!



My Answer:
>get('s');

$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
's' => $search,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'nickname',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'first_name',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search,
'compare' => 'LIKE'
),
array(
'key' => 'display_name',
'value' => $search,
'compare' => 'LIKE'
)
)
);

$query = new WP_Query($args);

if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
// Display post content
}
} else {
// No results found
}

Rate this post

4 of 5 based on 9027 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us