Site logo

[Resolved] Query loop – more than 1 post type

Home Forums Support [Resolved] Query loop – more than 1 post type

Home Forums Support Query loop – more than 1 post type

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2464580
    Dan

    I would like to display in my query loop more than one post type.
    For example Posts and Tutorials (a custom post type).
    Is that doable?

    Thanks!
    Dan

    #2464997
    David
    Staff
    Customer Support

    Hi there,

    you can using the generateblocks_query_loop_args filter hook to merge other paremeters. Add this snippet:

    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // Check your logic to apply the filtering only to specific loop
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'multi-post-type' ) !== false
        ) {
        // Merge the current $query_args which contains arguments defined in the editor with your ordering arguments
            return array_merge( $query_args, array(
              'post_type' => array('post', 'your_other_post-type'),
            ) );
        }
    
        return $query_args;
    }, 10, 2 );

    Update the post_type array.
    Then edit the Query Loop block, select the Grid block that is inside it and add the CSS Class of: multi-post-type

    #2465744
    Tom

    Hi David,

    I found this page via Google.

    I’ve tried the code above for a project I’m working on.

    However, it won’t save in functions.php – there’s an error:

    syntax error, unexpected '{'

    (on this line)
    ) {

    #2465877
    David
    Staff
    Customer Support

    Oops , good spot i fixed that in the code above ( removed extraneous && from the line before ).
    Sorry for any issues that may have caused.

    #2466305
    Dan

    Thanks David, tested and it’s working

    Dan

    #2466747
    David
    Staff
    Customer Support

    Glad to hear that!

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