Site logo

[Resolved] Post Template in Archive Page Hero filtered by Archive Title?

Home Forums Support [Resolved] Post Template in Archive Page Hero filtered by Archive Title?

Home Forums Support Post Template in Archive Page Hero filtered by Archive Title?

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #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?

    #2462254
    Fernando
    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-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

    #2462304
    Kip

    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

    #2462323
    Fernando
    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.

    #2462341
    Kip

    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.

    #2462354
    Fernando
    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.

    #2462359
    Kip

    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.

    #2462365
    Kip

    More private info

    #2462386
    Fernando
    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.

    #2462748
    Kip

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

    #2463509
    Fernando
    Customer Support

    You’re welcome, Kip!

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.