- This topic has 5 replies, 2 voices, and was last updated 3 years ago by
David.
-
AuthorPosts
-
March 16, 2023 at 3:23 am #2569639
Fabien
Hi,
I am using the following code in order to amend my related post element when the ACF Relationship field
articles_similairesis not empty :add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'related-posts' ) !== false ) { global $post; $related_posts = get_field('articles_similaires', $post->ID ); if ( $related_posts ) { $query_args['post_type'] = 'post'; $query_args['post__in'] = $related_posts; $query_args['orderby'] = 'post__in'; } } return $query_args; }, 10, 2 );It works well when the query loop as almost no settings (just post_type and post_per_page). However, when my query loop has more settings (example a query parameters on taxonomies), my snippet does not work anymore. I guess it conflicts with the current settings of the query block. Is there a way to simply override those settings when using
generateblocks_query_loop_args?March 16, 2023 at 5:14 am #2569766David
StaffCustomer SupportHi there,
you can merge the args using
array_merge– see here for example:add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'related-posts' ) !== false ) { global $post; $related_posts = get_field('articles_similaires', $post->ID ); if ( $related_posts ) { return array_merge( $query_args, array( 'post_type' => 'post', 'post__in' => $related_posts, 'orderby' => 'post__in', ) ); } } return $query_args; }, 10, 2 );March 16, 2023 at 8:14 am #2570096Fabien
Hi David,
Thanks for your reply.
Maybe I was not clear, what I am trying to achieve :
- If my ACF field (relationship) is filled, the query loop displays the selected posts
- Else, it displays posts of the same category of the current post.
In order to achieve the “else”, I was planning to use the settings of the query loop block but it seems to create a conflict with my “if”.
I hope this is clear for you ?
Fabien
March 16, 2023 at 10:02 am #2570191David
StaffCustomer SupportTry clearing the
tax_queryin your args eg.$query_args['tax_query'] => null;March 17, 2023 at 2:07 am #2570778Fabien
Thanks @David, I’ve gone another route (not using query block params except
post_typeandpost_per_page) :/** * Single - Custom Query for Related Posts */ add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'related-posts' ) !== false ) { global $post; $related_posts = get_field('articles_similaires', $post->ID ); if ( $related_posts ) { $query_args['post_type'] = 'post'; $query_args['post__in'] = $related_posts; $query_args['orderby'] = 'post__in'; } else { $query_args['post_type'] = 'post'; $query_args['tax_query'] = array( array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => wp_get_post_terms( $post->ID, 'category', array( 'fields' => 'ids' ) ), ), ); $query_args['post__not_in'] = array( $post->ID ); // Exclude current post $query_args['orderby'] = 'rand'; // Display posts randomly } } return $query_args; }, 10, 2 );Works as well.
March 17, 2023 at 4:24 am #2570901David
StaffCustomer SupportThat was going to be my final offering 🙂 Glad to hear that worked!
-
AuthorPosts
- You must be logged in to reply to this topic.