DougFromAccounting
DougFromAccounting

Reputation: 17

Limit Custom Post Type loop to 1 result

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?

$taxonomy=get_field('show_from');
                    $args = array(
                        'post_type' => 'project',
                        'tax_query' => array(
                                    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() ) {
                    $query->the_post();
                    $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' ); ?>

                    <?php 

                    echo "<div class='cpt-overlay'>";
                    echo "<div class='project-information'>"; 
                    echo "<h3>";
                        the_title();
                    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 {
            }?>

Upvotes: 1

Views: 119

Answers (1)

random_user_name
random_user_name

Reputation: 26160

This is a prime example of how proper indenting / formatting code makes a huge difference in your ability to diagnose / troubleshoot issues.

The problem is actually simple: The posts_per_page argument is in the wrong place - it's nested inside the tax_query, but needs to be at the "top level":

$args = array(
    'post_type'      => 'project',
    // moved this argument out of the tax query
    'posts_per_page' => 1,
    'tax_query'      => array(
        array(
            'taxonomy' => 'project-categories',
            'field'    => 'slug',
            'terms'    => $taxonomy
        )
    )
);

* Most good IDE's, such as PHPStorm, provide easy-to-use tools to auto-format your code for you - I'd strongly recommend finding and using one - I'm a huge fan of PHPStorm.

Upvotes: 1

Related Questions