php - How can I search for post's 'title','content'&'meta_query' without WP_Query 's'?

508

I have written a search query in wordpress that searches for 'post_title', 'post_content' & 'meta_query'. Instead of using WP_Query Search 's' parameter, can I use WP_Query's 'post' & 'meta_query' related parameters combine with 'OR' so that search query matches the 'post_title' and 'post_content' by passing only concerned parameters of WP_Query? OR is there a way to write query manually with $wpdb like below:

global $wpdb;
$query= "SELECT wp_dxwe_posts.* FROM `wp_posts` WHERE 
(wp_posts.post_status IN ('$status_array') AND
wp_posts.post_type='messages') AND 
(wp_posts.post_title LIKE '%$keyword%' OR wp_posts.post_content 
LIKE '%$keyword%') AND ( wp_postmeta.meta_key == 'my_meta_key' AND 
wp_postmeta.meta_value LIKE '%$keyword%') ";

$my_query = $wpdb->get_results($query);

Can any one please tell me how? Thanks in advance.

I am getting the following MySql result query:

  SELECT * FROM `wp_dxwe_posts` wp 
INNER JOIN `wp_dxwe_postmeta` wm ON (wm.`post_id` = wp.`ID`) 
WHERE wp.`post_status` IN ('publish,pending,draft') 
AND wp.`post_type`='messages'
AND (wp.`post_title` LIKE '%prasad%' OR 
wp.`post_content` LIKE '%prasad%') 
    AND wm.`meta_key` = 'inistitute_name' 
    AND wm.`meta_value` LIKE '%some_institute_name%'
905

Answer

Solution:

Use custom query in following way :

SELECT * FROM `wp_posts` wp 
    INNER JOIN `wp_postmeta` wm ON (wm.`post_id` = wp.`ID`) 
        WHERE wp.`post_status` IN ('$status_array') 
        AND wp.`post_type`='messages'
        AND (wp.`post_title` LIKE '%$keyword%' OR 
wp.`post_content` LIKE '%$keyword%') 
        AND wm.`meta_key` = 'my_meta_key' 
        AND wm.`meta_value` LIKE '%$keyword%'

People are also looking for solutions to the problem: php - Insert data in mysql table

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.