I’m working on a WordPress site with a large product database. I need to make a listing of all the products with a single taxonomy term exception. Using wp_query() creates a huge object and takes a long time. Also, for some reason, my arguments will not exclude products from the one taxonomy term I don’t want listed. Here is my current query:

$args = array(
    'post_type' => 'products',
    'post_status' => 'publish',
    'orderby' => 'post_title',
    'numberposts' => -1,
    'tax_query' => array(
        'taxonomy' => 'product-main-cats',
        'field'    => 'term_id',
        'terms'    => array(23),
        'operator' => 'NOT IN',
    ),
);
$prods = new WP_Query( $args );

I want to rewrite this query using the wpdb class so that I only fetch exactly what I need for my listing instead of the huge wp_query() object. The only columns I need from the wp_post table are, ID and post_title. I’m just not sure of the mySQL syntax, especially for excluding the posts related to the single taxonomy term. Can anyone help me rewrite this?

TIA!

Read more here: Optimizing a WordPress Query


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: