1

When I add a new WP_Query, everything works, but if I add a 'tax_query' with a taxonomy, it doesn't pull the posts. post_count would always be 0.

    $products = new WP_Query([
        'post_type' => 'post',
        'tax_query' => [
            [
                'taxonomy' => 'show_product_on_only_premium',
                'field'    => 'slug',
                'terms'    => 'yes',
                'operator' => 'AND',
            ]
        ]
    ]);

    echo var_dump($products);

I've played with the tax_query with different values and I've had no luck. This works on production but not locally and I can't figure out why. edit> my database has the taxonomy

Please help! Thank you in advance!

  • 1
    the array syntax with bracket for php <5.4 does not work see stackoverflow.com/questions/17772534/… maybe it's the problem? – Andrea Somovigo Jul 26 '18 at 23:54
  • 1
    It might be the operator field, but you shouldn't need an entire tax_query for this – Tom J Nowell Jul 26 '18 at 23:58
  • 1
    As an aside, avoid NOT type operators like the plague, the cost for the DB is extreme, whereas filtering out in PHP or with multiple API calls, is significantly faster – Tom J Nowell Jul 27 '18 at 0:02
  • 1
    Are you sure these are posts of type post and not product? And that it's yes and not Yes? That the posts publish date are all in the past, and that they all have the publish post status? And that no pre_get_posts filters are messing with the query? – Tom J Nowell Jul 27 '18 at 0:10
  • 1
    Are these WooCommerce products? Or a custom post type? Clearly what you're querying for is not what you have, how is the show_product_on_only_premium taxonomy registered? – Tom J Nowell Jul 27 '18 at 0:32
1

Your query can be simplified to this:

$products = new WP_Query([
    'post_type' => 'post',
    'show_product_on_only_premium' => 'yes',
]);

echo var_dump($products->posts);

The culprit was likely the 'operator'=>'AND', the default value is IN, so changing your original query to use IN instead of AND would also work, as would removing the operator parameter as the default is also IN

  • Thank you for the tip, @TomJNowell. I'll definitely keep that in mind. Unfortunately putting it as 'show_product_on_only_premium' => 'yes' didn't help but damn is that really good to know I could just do something like that. Thank you! – PepperAddict Jul 27 '18 at 0:05
  • Tom, it will work only if this taxonomy registers query variable ;) – Krzysiek Dróżdż Jul 27 '18 at 14:37
1

I was able to get it to work by just avoiding post_type since I had no idea what it was.

   $products = new WP_Query( array(
        'tax_query' => array(
            array(
                'taxonomy' => 'show_product_on_only_premium',
                'field'    => 'slug',
                'terms'    => 'yes',
            ),
        ),
    ) );

I tried to do it again with another taxonomy and I was so frustrated that it was still giving me no posts but it worked in production. Well it turns out my database locally has count: 0 but in production has count: 7. face palm

1

You can use below code

   $products = new WP_Query( array(
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'show_product_on_only_premium',
                    'field'    => 'slug',
                    'terms'    => 'yes',
                ),
            ),
    ) );
  • 1
    'relation' => 'AND' it is not necessary here. – nmr Jul 27 '18 at 6:03
  • 1
    Yes i agree with you but above example 'operator' => 'AND', written – Trilok Jul 27 '18 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.