• Hello everyone! I have multiple custom post types that I’d like to include in one query loop. However, the current Query Loop block doesn’t have the option to include more than one. It’s either post OR page OR custom post type.

    Is it possible to include more than one post type? For instance, post AND page AND custom post type.

    Thank you for your help!

    Al

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @thepilot,

    So sorry for the challenge. Let me use code to explain what you can do.

    Assuming you have two post types: testimonial and post. What you need to do is to add an array that will loop through these post types. Here’s some sample code, then I’ll explain:

    <?php
    // Code starts here. Calls the post types (2 in this case)
    $args = array(
        'post_type'      => array( 'testimonial', 'post' ),
        'posts_per_page' => 1,
    );
    
    $query = new WP_Query( $args );
    
    $postcount = 0;
                ?>
                <?php if ($query->have_posts()) : ?>
                    <?php while ($query->have_posts()) : $query->the_post(); ?>
                        <?php $postcount++; ?>
    //looped content here
    <?php wp_reset_query(); ?>
    
    

    The first part of the code pulls the array of post types to be presented. (testimonial and post in this case)

    The second part then loops through and presents the content, if any.

    Hope that helps! And do let me know if you’ll need further help.

    Thread Starter ThePilot

    (@thepilot)

    Thank you very much @skyoyugi !

    Where would I paste this code exactly? In the functions.php file? Actually I’d like to use the Query Loop block, because it’s easier to style with the block editor. Is this how it will work?

    Thank you so much for your help,

    Al

    Thread Starter ThePilot

    (@thepilot)

    Hey @skyoyugi, sorry for asking again… Is this possible to do with the query loop *block* ? Ask that block to display more than one post type? Thank you very much for your help

    the way i see it, this is not possible, as the request goes through the rest api, and there you cannot query for multiple posts at once (‘/wp-json/wp/v2/post-type/’).

    you would need to code your own block.

    another hacky solution would be, to overwrite the output of your block via add_filter('render_block_core/query', 'support_render_core_query', 10, 3); and then write your own wp_query and manipulate the output of the mentioned filter. this is hacky, since you woudnt see the result in the block editor, only in the frontend.

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