Site logo

Archived topic

Post Template in Archive Page Hero filtered by Archive Title?

10 replies · Started by Kip on December 14, 2022

Viewing posts 1–11 of 11

I am working to build an archive page, with some custom content in a Page Hero that includes a pattern of 3 posts filtered by a tag (Trending), so for each archive page, a Query Loop select 3 posts first by the Archive name, then by the Trending tag. So for example on the archive page "How To" (a category), the most recent 3 posts tagged Trending are displayed. That's the desired outcome, anyway. However...
If I select "Inherit query from template" in the query loop settings, this works (it filters the posts by the archive). However it does not respect the "posts per page" setting (it disappears when you enable "inherit query from template"). So if there are say 12 posts tagged Trending in the archive filter, it will show all 12 posts.
If I turn off "inherit query from template," the opposite happens: I get the desired 3 posts, but they are not filtered by the Archive, so the 3 most recent posts of any category/tag that are tagged "Trending" show up.
Can I get both of these parameters working at once?

Hi Kip,

The “Inherit query from template” option makes the Query loop have the same query as the main query. Thus, it copies the "posts per page" value.

You can try altering this by adding cu-query-loop to the class list of the Grid Block of the Query loop Block.

Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

Then, add this snippet:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-query-loop' ) !== false ) {
		$query_args['posts_per_page'] = 3;
    }

    return $query_args;
}, 10, 2 );

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Hey thanks :)
I can't say that this is the most intuitive solution but it certainly works! Appreciate it.
Sorry I spoke too soon. The filter needs to also account for the "Trending" tag, as adding parameters is also unavailable with the "Inherit query from template" enabled. Afraid I'm not quite sure how to amend the function to add the parameter. Can you help?

kip

Actually, replace the code with this:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-query-loop' ) !== false ) {
		$category = get_category( get_query_var( 'cat' ) );
		$query_args['tax_query'] = array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $category->slug,
            ),
        );
    }

    return $query_args;
}, 10, 2 );

Now, disable "Inherit query from template" and set your parameters.

Really appreciate all the help.
Not quite there yet though. The parameter I am using to filter is a Tag ("Trending"), not a category.
I tried replacing the category references with tag, like this:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-query-loop' ) !== false ) {
$tag = get_tag( get_query_var( 'tag' ) );
$query_args['tax_query'] = array(
array(
'taxonomy' => 'tag',
'field' => 'slug',
'terms' => $tag->slug,
),
);
}

return $query_args;
}, 10, 2 );
but it did not work either. The Query Loop just returns the 3 most recent posts in the archive list for each particular archive.
Update: I went back, using your most recent filter, and tried adding a Category parameter to the Query Loop, but it too only returned the most recent 3 posts from an archive list, so the code itself isn't working either for category or tag.
I can think about other ways to handle this, it's not a huge deal if it's too much trouble.

Yes the cu-query-loop class is set on the Grid, set to the category of Surface, but it doesn't seem to make any difference. I

I am calling it a night, I have left login instructions for you in Private Information.

More private info

I see. Can you try this code instead?:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-query-loop' ) !== false && ! is_singular() ) {
$category = get_category( get_query_var( 'cat' ) );
		$query_args['tax_query'] = array(
'relation' => 'AND',
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'hero',
            ),
array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $category->slug,
            ),
        );
    }
    return $query_args;
}, 10, 2 );

Remove the taxonomy Parameter you have.

This sets the query to have a taxonomy of the current category, and tag of trending.

Let us know how it goes.

Yes thank you, this does appear to be working now :).
Appreciate all the assistance and you can finally close this ticket now.

You're welcome, Kip!

This archived topic is closed to new replies.