0
votes

I have set up a query to search through some custom post types that I have set up. I can get it to show all the post so I know it is searching the custom post types that I want. But what I cant figure out is how to have it just return ONE post from each custom post type that I am searching.

Example: I have 4 custom post types. CPT1, CPT2, CPT3, CPT4. I want to get one post from CPT1, one post from CPT2, etc. etc. so that way in the loop it would be: title of CPT1, title of CPT2, etc etc.

Then I figured I could do a query for each custom post type and make it each have their own loop, but that seems like a lot of code for that. Thank you for any help in advanced.

This is the code I have now:

$mystate = get_state();


// args
$args = array(
    'numberposts'   => -1,
    'post_type'     => array('CPT1','CPT2','CPT3','CPT4'),
    'meta_key'      => 'state_420',
    'meta_value'    => $mystate,
    'order' => 'ASC',

);

/ query
$the_query = new WP_Query( $args );

?>

<?php
        if ( $the_query->have_posts() ) : ?>

            <?php
            /* Start the Loop */
            while ( $the_query->have_posts() ) : $the_query->the_post();

                /**
                 * Run the loop for the search to output the results.
                 * If you want to overload this in a child theme then include a file
                 * called content-search.php and that will be used instead.
                 */
                ?>

                <li style="display: inline-block;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>, </li>
<?php

            endwhile;


             the_posts_pagination( array( 'mid_size' => 2 ) );

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
1

1 Answers

0
votes

You should pass this argument posts_per_page=>1