- This topic has 10 replies, 2 voices, and was last updated 3 years, 4 months ago by
Fernando.
-
AuthorPosts
-
December 14, 2022 at 4:28 pm #2462219
Kip
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?December 14, 2022 at 5:57 pm #2462254Fernando Customer Support
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-loopto 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
December 14, 2022 at 7:43 pm #2462304Kip
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
December 14, 2022 at 8:36 pm #2462323Fernando Customer Support
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.
December 14, 2022 at 9:06 pm #2462341Kip
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.December 14, 2022 at 9:23 pm #2462354Fernando Customer Support
That’s odd. I tested the code from my end on Category Archive Pages and it worked. Did you add the “cu-query-loop” to the class list of the Grid Block as instructed?
Can you provide the link to the Category Archive page you tested? You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
We’ll deal with the tags afterward.
December 14, 2022 at 9:39 pm #2462359Kip
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.
December 14, 2022 at 9:52 pm #2462365Kip
More private info
December 14, 2022 at 10:56 pm #2462386Fernando Customer Support
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.
December 15, 2022 at 6:24 am #2462748Kip
Yes thank you, this does appear to be working now :).
Appreciate all the assistance and you can finally close this ticket now.December 15, 2022 at 5:10 pm #2463509Fernando Customer Support
You’re welcome, Kip!
-
AuthorPosts
- You must be logged in to reply to this topic.