Archived topic
Query loop block // Override existing settings
5 replies · Started by Fabien on March 16, 2023
Hi,
I am using the following code in order to amend my related post element when the ACF Relationship field articles_similaires is 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 ?
Hi 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 );
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
Try clearing the tax_query in your args eg.
$query_args['tax_query'] => null;
Thanks @David, I've gone another route (not using query block params except post_type and post_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.
That was going to be my final offering :) Glad to hear that worked!