- This topic has 15 replies, 4 voices, and was last updated 3 years, 3 months ago by
Anastasiya.
-
AuthorPosts
-
December 20, 2022 at 6:36 pm #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;?>December 20, 2022 at 6:48 pm #2468859Fernando Customer Support
Hi Anastasia,
Are you planning to show the favorite posts on a Static page or an archive page?
December 21, 2022 at 7:54 am #2469558Anastasiya
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.
December 21, 2022 at 9:04 am #2469643David
StaffCustomer SupportHi 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
Customand addyour_custom_hook_nameDecember 21, 2022 at 9:21 am #2469667Anastasiya
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?December 21, 2022 at 9:29 am #2469677David
StaffCustomer SupportOk, that code should work, apart from the
get_user_favorites();function it all looks pretty standardBUT 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-nameThen you can use the
generateblocks_query_loop_argsfilter 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 );December 21, 2022 at 11:04 am #2469754Anastasiya
David, I tried your second solution (Query loop) and it worked like a charm. Thank you!
December 21, 2022 at 2:49 pm #2469906Anastasiya
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.)December 22, 2022 at 1:50 am #2470196David
StaffCustomer SupportHmmm…. 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 ?
December 22, 2022 at 9:30 am #2470741Anastasiya
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.)December 23, 2022 at 3:51 am #2471371David
StaffCustomer SupportCan i see the loop where the $favourites aren’t working on pagination ?
December 23, 2022 at 9:07 am #2471759Anastasiya
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.
December 24, 2022 at 4:54 am #2472236David
StaffCustomer SupportBefore we do that, i have asked Tom and Jean to have a look, we may need to filter that param in differently.
December 26, 2022 at 8:19 am #2473867Anastasiya
Thank you David. Let me know if you need anything from me while you guys are taking a look.
December 31, 2022 at 8:27 pm #2479283Tom
Lead DeveloperLead DeveloperHi there,
This is a bug in GenerateBlocks right now where the
generateblocks_query_loop_argsfilter doesn’t work for pagination.We’ll be fixing it in 1.7. Sorry for the inconvenience!
-
AuthorPosts
- You must be logged in to reply to this topic.