Query Loop Category Parameter Based on Current Page

I may be missing something but cannot seem to get a Query Loop to display posts from the category the user is reading. ie. if someone was on page A under Category 1, I want the Query Loop to display 6 posts from Category 1. I know you can choose Taxonomy = Specific Category but was hoping there was a glabal setting to make it work dynamically (ie. Taxonomy = Current Post or something to that effect)?

Add-On: I think I found more info. On this KB: Creating Related Posts with Query Loop Block - GenerateBlocks

Under " Related posts by taxonomy" I see:

Taxonomy: Categories
Select Terms: Current post terms
Exclude posts: Exclude current post

I have chosen:

Taxonomies = Categories
Select terms = Current post terms

But this does not serve any posts.

I can even choose Source > Current post terms but this also does not serve any posts.

What am I missing?

Wait a minute, never fails. The solution presented itself after I create this topic.

I think I may have found the issue. The Query Loop is on a Page, with which there are no Categories! Is there a way to tag a Page with a current Category or is that a bad idea and will mess with other Loops pulling in Category info?

May just have to use the manual method then.

Thx!

You can try registering categories for pages:

I have not tested this but as its a shared category it may work.

1 Like

Thanks David!

I will test that. Do you think this will cause /category/ to also display these pages or does core category only display Posts?

I am use this in conjunction with Element Control to make things easier. Is there a way for a Block Element to figure to only display itself (a button in this case) if the specific category has more than 6 posts?

Pages won’t automatically show in the default archives you would need to use pre_get_posts hook to do that eg.

add_action( 'pre_get_posts', function( $query ) {
    if ( $query->is_main_query() && ! is_admin() && ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) ) {
        $query->set( 'post_type', array( 'post', 'pages' ) );
    }
});

For the GP Elements question - you would need to use the generate_element_display filter to change its display conditions:

Might want to ask over at GPP Support for that.

1 Like

Awesome, let me continue with this. Thanks so much!