Site logo

[Support request] Use Content Block Elements for s Custom loop

Home Forums Support [Support request] Use Content Block Elements for s Custom loop

Home Forums Support Use Content Block Elements for s Custom loop

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #2468854
    Anastasiya

    Hi,
    I am rebuilding a site using Generatepress Pro and GenerateBlocks PRO.The original site had a custom loop and custom content template to display user favorites posts (Favorites Plugins in WP repository). I am including the loop at the bottom of the topic. In the GP-rebuilt theme I am using Elements (Content Blocks) to create custom layouts for posts on archive pages. I want to use the same content block template to display user favorites but can’t figure out a way to create this new loop using Generatepress and GenerateBlacks. Can you point me in the right direction on how to create the desired output with GP?

     
    <?php
    $favorites = get_user_favorites();
    if ( $favorites ) : // This is important: if an empty array is passed into the WP_Query parameters, all posts will be returned
    
    $favorites_query = new WP_Query(array(
    	'post_type' => 'vimeo-video', // If you have multiple post types, pass an array
    	'posts_per_page' => -1,
    	'ignore_sticky_posts' => true,
    	'post__in' => $favorites,
    	'paged' => $paged // If you want to include pagination, and have a specific posts_per_page set
    ));
    if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
    	
    get_template_part( 'loop-templates/content', 'video' );
    endwhile; 
    
    endif; wp_reset_postdata();
    
    else : ?>
        <p>You have not picked any favorite workouts yet in the video library. Please choose some and they will appear in this personalized playlist. </p>
         
    <?php endif;?>
    #2468859
    Fernando
    Customer Support

    Hi Anastasia,

    Are you planning to show the favorite posts on a Static page or an archive page?

    #2469558
    Anastasiya

    I will be showing the Favorites on a Static page. I was thinking about using a Query Loop block. But I still need a way to modify the output of the loop by adding the favorites parameter to it.

    #2469643
    David
    Staff
    Customer Support

    Hi there,

    a simple solution would be to replace:

    get_template_part( 'loop-templates/content', 'video' );

    and add a Hook eg.

    do_action('your_custom_hook_name'):

    Then you could switch your GB Block Content Template to a Hook, and set the hook to Custom and add your_custom_hook_name

    #2469667
    Anastasiya

    Would this work when using Generatepress theme?
    The code that I posted is from a custom theme that didn’t use GeneratePress or GenerateBlocks.
    Currently I have posts displayed in columns on archive pages. Do you think that the solution that you proposed would also use the column layout or will I need to add extra filters for it?

    #2469677
    David
    Staff
    Customer Support

    Ok, that code should work, apart from the get_user_favorites(); function it all looks pretty standard

    BUT in my haste i didn’t suggest the alternative with query look block.
    You can build the query loop block, give the Grid inside the query loop an Advanced > Additional CSS class eg. my-class-name

    Then you can use the generateblocks_query_loop_args filter hook to pass in your own params to any query loop block with that class:

    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    
        // get the favorites
        $favorites = get_user_favorites();
    
        // Check your logic to apply the filtering only to specific loop
        if (
            $favorites &&
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'my-class-name' ) !== false
            ) {
                // Merge the current $query_args which contains arguments defined in the editor with your ordering arguments
                return array_merge( $query_args, array(
                'post__in' => $favorites,
            ) );
        }
    
        return $query_args;
    }, 10, 2 );
    #2469754
    Anastasiya

    David, I tried your second solution (Query loop) and it worked like a charm. Thank you!

    #2469906
    Anastasiya

    I ended up having one more questions regarding the query loop.
    If I try to add pagination to the Query Loop block the Favorites parameter is not applied to is. The pagination displays the entire number of pages for the specific custom post type and when I go to the next page no results are returned. Is there a way to use the pagination correctly with the Query Loop?
    On Archive pages I am using Infinite scroll with a Load More button (this would be ideal behavior for the Favorites display page as well.)

    #2470196
    David
    Staff
    Customer Support

    Hmmm…. i will need to check as to why that custom parameter, but as the Query Loop Blcok doesn’t have infinite scroll yet, its something we’re looking at for GB Pro, a quick question:

    Where is this custom loop being displayed ? Is it a single static page or is it in other content ?

    #2470741
    Anastasiya

    The custom block is displayed on a static page. It is displaying a custom post loop.
    I am fine with paged pagination as well, as long as it works 🙂 (Already using GB Pro, so it would be great to see the infinite scroll feature in the future.)

    #2471371
    David
    Staff
    Customer Support

    Can i see the loop where the $favourites aren’t working on pagination ?

    #2471759
    Anastasiya

    It’s a membership site. Do you want me to create a temporary login for you to see the loop? Favorites are also for logged-in members only so in order to see the loop you must have an account on the website.

    #2472236
    David
    Staff
    Customer Support

    Before we do that, i have asked Tom and Jean to have a look, we may need to filter that param in differently.

    #2473867
    Anastasiya

    Thank you David. Let me know if you need anything from me while you guys are taking a look.

    #2479283
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This is a bug in GenerateBlocks right now where the generateblocks_query_loop_args filter doesn’t work for pagination.

    We’ll be fixing it in 1.7. Sorry for the inconvenience!

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