Support » Fixing WordPress » Limit Custom Post Type loop to 1 result

  • Resolved fruskator


    I have a wordpress page in which I need to load a custom post type at the bottom. There are 8 categories of post types, and I have the loop working so that it pulls from the correct category based on the page that loads. The problem is that I want to limit it to just the latest post in that category. Right now the loop is pulling all posts from a category, and I just want 1. I’m not sure how to limit it in the array. I’ve tried posts, posts_per_page, and num_pages, but none of them seem to work. Does anyone know the value that I’m missing?

                        $args = array(
                            'post_type' => 'project',
                            'tax_query' => array(
                                    'taxonomy' => 'project-categories',
                                    'field' => 'slug',
                                    'posts_per_page' => 1,
                                    'terms' => $taxonomy
                    $query = new WP_Query( $args ); ?>
                <?php if ( $query->have_posts() ) {
                    while ( $query->have_posts() ) {
                        $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
                        <div class="cpt-feature" style="background: url('<?php echo $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
                        <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
                        echo "<div class='cpt-overlay'>";
                        echo "<div class='project-information'>"; 
                        echo "<h3>";
                        echo "</h3>";
                        echo "<p>";
                        echo get_field('intro_blurb');
                        echo "<p><a href='" . get_permalink() . "'>View Project</a></p>";
                        echo "</div><!--project-info-->";
                        echo "</div><!--cpt-overlay-->";
                        echo "</div><!--cpt-feature-->";
                        echo "</a>";
                } else {
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Limit Custom Post Type loop to 1 result’ is closed to new replies.