Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress get_posts if keyword exists in title, content or tag

I am using get_posts to get a list of posts that match a search keyword, the problem is that I the get_posts's s parameter does not search tags by default and using tag_slug__in will not work if the keyword is found in the title and not in a tag.

The conditions of my search would be:

  1. Return post if keyword exists in Title
  2. Return post if keyword exists in Content
  3. Return post if keyword is a tag associated with the post.

Any ideas would be fantastic. I tried the "Search Everything" plugin, but that only seems to work on WordPress' default Search Feature.

Code below is a simplified version of what I have attempted, not this does not satisfy all 3 criteria.

<?php

/* Set global query parameters */
$image_args = array(
    'posts_per_page' => (isset($_GET['show_all']) && $_GET['show_all'] == 'true')? 100000 : 20,
    'post_type' => 'attachment',
    'post_mime_type' => array('image/jpeg', 'image/png'),
    'meta_key' => 'language',
    'meta_value' => "(^".$languages."$|\"".$languages."\"\;)",
    'meta_compare' => 'REGEXP',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'media_category',
            'field' => 'term_id',
            'terms' => array($gallery_filter, $hotel->term_id),
            'operator' => 'AND',
        ),
    ),
);

/* If page numbert given, add offet */
if(!empty($page_no))
    $images_args['offset'] = 20*((int)$page_no-1);


/* If search term submitted, add it to the s parameter */
if(isset($_GET['search'])){
    $image_args['tag_slug__in' = explode(" ", $_GET['search']);
    $image_args['s'] = urldecode($_GET['search_term']);
}
like image 640
Stefan Dunn Avatar asked Jul 24 '15 14:07

Stefan Dunn


2 Answers

you can use your own query . below is an example

$querystr="
    SELECT *
        FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy, $wpdb->terms
            WHERE ($wpdb->terms.name = '$s'
            OR $wpdb->posts.post_content LIKE '%$s%'
        OR $wpdb->posts.post_title LIKE '%$s%')
        AND $wpdb->posts.ID = $wpdb->term_relationships.object_id
        AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
        AND $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
        ORDER BY $wpdb->posts.post_date DESC
";

$pageposts = $wpdb->get_results($querystr, OBJECT_K);

foreach ($pageposts as $post): setup_postdata($post);
    echo the_title() . '<br /><br />';
endforeach;
like image 165
Manoj Dhiman Avatar answered Oct 10 '22 06:10

Manoj Dhiman


I have used wpdb query to fetch the result. Join the posts and taxonomy table and then get the distinct result by $search_title. Hope this will work. :)

global $wpdb;
$querystr= "SELECT DISTINCT $wpdb->posts.* FROM $wpdb->posts
                LEFT JOIN $wpdb->term_relationships
                    ON ( $wpdb->posts.ID = $wpdb->term_relationships.object_id )
                LEFT JOIN $wpdb->term_taxonomy
                    ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id )
                LEFT JOIN $wpdb->terms
                    ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ) 

                WHERE ( $wpdb->terms.name LIKE '%$search_title%'
                    OR $wpdb->posts.post_content LIKE '%$search_title%'
                    OR $wpdb->posts.post_title LIKE '%$search_title%' )
                    AND $wpdb->posts.post_status = 'publish'
                    AND $wpdb->posts.post_type = 'product' 
                    ORDER BY $wpdb->posts.post_title ASC";

$query_object = $wpdb->get_results($querystr);
like image 36
Faysal Haque Avatar answered Oct 10 '22 06:10

Faysal Haque