2

I have a category archive page (press), which I need to loop through posts of a certain tag (magazines). I tried to modify the query with pre_get_posts, but it is still just showing posts in the 'press' category. I got this to work with a custom query setting the tag in $args, but then I lose pagination.

To clarify, posts with the tag 'magazine' may or may not have the category 'press', and I need to loop through EVERY post with the tag 'magazine'.

Is there anything wrong with my code? Ideas how to accomplish this?

function tag_loop( $query ) {
    if ( $query->is_category() && $query->query_vars['cat'] == 'press' && $query->is_main_query()) {
        $query->set( 'tag', 'magazine' );
    }
}
add_action( 'pre_get_posts', 'tag_loop' );
  • Did you try unsetting the cat parameter, setting it as null for example? – Felipe Elia Dec 28 '17 at 11:43
  • I did, no luck. Tried various iterations of the following: unset( $query->query_vars['cat'] ); $query->set('cat', ''); $query->unset('cat'); But when I do a dump it vehemently keeps the same category. – website walrus Dec 28 '17 at 20:06
  • A good move would be use $query->query_vars; die(); to see which parameter is being used to set the category... did you try that yet? If you do that and don't know what to do next, just paste the content as a edit to you original question :) – Felipe Elia Dec 30 '17 at 13:53
0

i had a similar issue, where i wanted to show different subcartegories inside a main category

  • shoes
  • men
    • shoes men
  • women
    • shoes women

so inside shoes i wanted to show both men and women shoes whilst non of these items should be required to be in category shoes..

your tax_query might be correct but you have to look inside your query for more query variables.

use echo '<pre>', print_r( $query ), '</pre>'; inside your pre_get_posts function.

in my case, the very first entry was [product_cat] => shoes so i wrote unset( $query->query_vars['product_cat'] ); to remove that entry, and that worked on my end.

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.