Site logo

[Resolved] Query loop block // Override existing settings

Home Forums Support [Resolved] Query loop block // Override existing settings

Home Forums Support Query loop block // Override existing settings

  • This topic has 5 replies, 2 voices, and was last updated 3 years ago by David.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2569639
    Fabien

    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 ?

    #2569766
    David
    Staff
    Customer Support

    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 );
    #2570096
    Fabien

    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

    #2570191
    David
    Staff
    Customer Support

    Try clearing the tax_query in your args eg.

    $query_args['tax_query'] => null;

    #2570778
    Fabien

    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.

    #2570901
    David
    Staff
    Customer Support

    That was going to be my final offering 🙂 Glad to hear that worked!

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