Site logo

Archived topic

Query loop - more than 1 post type

5 replies · Started by Dan on December 16, 2022

Viewing posts 1–6 of 6

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

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

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)
) {

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

Thanks David, tested and it's working

Dan

Glad to hear that!

This archived topic is closed to new replies.