Custom wpdb query for select post more than one taxonomy

I’m trying to write a custom select post query for wordpress. I’m using woocommerce. In woocommerce the default post type name is “product” and taxonomy name in “product_cat”
I’ve added another taxonomy names “spec” for product specification.

So in my product category assume “samsung”, “laptop” is a term name for product category. and in “spec” I’ve some specification such as its “core i5 processor”, “8gb ddr3 ram” etc. and my post title is “super notebook”

Read More

I want to search it for any of this field like if type notebook in my search field it will show all notebook. Underneath the search input type text area I also have a select box which has all “product_cat” there like samsung, hp, apple, laptop etc.

Product title / Post title

  • Super notebook

product_cat

  • samsung
  • laptop

spec

  • core i5 processor
  • 8gb ddr3 ram

    global $wpdb;

    $query = "SELECT DISTINCT wp_posts.*
                FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
                WHERE (wp_terms.name LIKE '%ddr3 ram%'
                    OR wp_posts.post_title LIKE '%ddr3 ram%')
                AND wp_posts.post_status = 'publish'
                AND wp_posts.post_type = 'product'
                AND wp_posts.ID = wp_term_relationships.object_id
                AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
                AND wp_term_taxonomy.term_id = wp_terms.term_id  GROUP BY  wp_posts.ID ORDER BY wp_posts.post_title ASC";
    
    $object = $wpdb->get_results($query);
    
    _log($object);
    

I try this code it show the result.

But if I try this it’ll not show the result

global $wpdb;

    $query = "SELECT DISTINCT wp_posts.*
                FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
                WHERE (wp_terms.name LIKE '%ddr3 ram%'
                    AND wp_terms.name LIKE '%samsung%'
                    OR wp_posts.post_title LIKE '%ddr3 ram%')
                AND wp_posts.post_status = 'publish'
                AND wp_posts.post_type = 'product'
                AND wp_posts.ID = wp_term_relationships.object_id
                AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
                AND wp_term_taxonomy.term_id = wp_terms.term_id  GROUP BY  wp_posts.ID ORDER BY wp_posts.post_title ASC";

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

    _log($object);

I don’t have any idea to fix this. So anyone want to help me please give this answer for me.
Thanks.

Related posts

Leave a Reply

1 comment

  1. You can try this

    global $wpdb;
    
    $query = "SELECT DISTINCT wp_posts.*
                FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
                WHERE (wp_terms.name LIKE '%ddr3 ram%'
                    OR wp_terms.name LIKE '%samsung%'
                    OR wp_posts.post_title LIKE '%ddr3 ram%')
                AND wp_posts.post_status = 'publish'
                AND wp_posts.post_type = 'product'
                AND wp_posts.ID = wp_term_relationships.object_id
                AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
                AND wp_term_taxonomy.term_id = wp_terms.term_id  GROUP BY  wp_posts.ID ORDER BY wp_posts.post_title ASC";
    
    $object = $wpdb->get_results($query);
    
    _log($object);