0

I am trying to find a way to combine a meta_query and a term_query and i got confused with the research.

    $sport = 'basket';
    $args_p = array(
                    'posts_per_page'      => 10,
                    'post_type' => 'user',
                    'no_found_rows'       => true,
                    'post_status'         => 'publish',
                    'meta_key' => 'user_overall_rating',
                    'tax_query' => array(
                              array(
                                  'taxonomy'  => 'restricted-sport',
                                  'field'     => 'slug',
                                  'terms'     => array($sport)
                                   'operator'  => 'NOT IN')    
                    ),
                    'orderby'  => 'meta_value_num',
                    'order'    => 'DESC'
                );
                $r = new WP_Query( apply_filters( 'widget_posts_args',$args_p  ) );
if (r->have_posts()):...

So i want to filter and get the top 10 users that does not have '$sport' basket as restricted sport. There are post_types that have taxonomies 'basket' ,'soccer' etc so i expected to get 10 top from the rest sports, but i get an error :) PHP Fatal error: Uncaught Error: Call to a member function have_posts() on null in widget-5.php "r->have_posts()"

7
  • I've read your post but I don't quite understand. You're looking to exclude posts assigned to a specific term from a 2nd query, is that correct? If not could you summarize in a comment below please?
    – Howdy_McGee
    Commented May 5, 2020 at 17:40
  • Hey because i am tottaly new i have confused many things, When i implemented the code i didnt know how to do and filter the taxonomies ,so i did two queries, which is not the proper way as i can imagine. I found out that i have to use tax_query but i am trying now to implement it and it doesnt run correctly, i will revise the question please check it within 2-3 minutes, i will make it more clear
    – Kap40
    Commented May 5, 2020 at 17:47
  • Note that NOT IN is extremely slow and very, very heavy on the database. It would be much faster to list every restricted sport except the one you don't want. Avoid parameters that exclude like the plague for performance reasons. Also you mention you get an error, but you never say what the error is, can you edit your Q to clarify what it was?
    – Tom J Nowell
    Commented May 5, 2020 at 17:56
  • i understand regarding the NOT IN , that the reason that i am seeking a better solution , until now i dont have many post_type 'user' and it was not causing any issue...
    – Kap40
    Commented May 5, 2020 at 18:01
  • PHP Fatal error: Uncaught Error: Call to a member function have_posts() on null in widget-5.php
    – Kap40
    Commented May 5, 2020 at 18:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.