Reputation: 135
I'm very well aware of the fact that this question has already been asked a million times, but I really need your help with it, because despite having followed all the recommendations, I can't limit the number of the custom post type to be shown to the number of 3. Meaning that, every time I create a new custom post type (a new marathon), the loop adds it to the others. What I want is my loop to only show the last 3 marathons. I thought it was enough to indicate 'posts_per_page' => 3, but it's not.
Please help! Thank you!
Here's my code:
<?php
$the_query = new WP_Query( 'post_type=kinsta_marathon' );
array(
'post_type' => 'kinsta_marathon',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'slider',
'field' => 'slug',
'terms' => 'slider'
)
)
);
// The Loop!
if ($queryObject->have_posts()) {
?>
<?php
while ($queryObject->have_posts()) {
$queryObject->the_post();
?>
<div class="container mb-3 py-3">
<div class="row h-100 pl-3 rowcalendartop ">
<div class="container h-100">
<div class="row h-100">
<div class="col-1 py-0"><img class="logomarathoncalendar" src="<?php the_field('logo_marathon'); ?>" alt="logo-marathon"></div>
<div class="col-10 py-0 align-self-center"><h5 class="mb-0 align self-center"> <?php the_title(); ?></h5></div>
</div>
</div>
</div>
<div class="row rowcalendarbottom h-100 greysection py-3">
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-map-marker-alt iconslidermarathon mr-3"></i> <?php the_field('where_marathon'); ?></h6></div>
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-calendar iconslidermarathon mr-3"></i> <?php the_field('when_marathon'); ?></h6></div>
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-running iconslidermarathon mr-3"></i> <?php the_field('km_marathon'); ?></h6></div>
<div class="col-2 align-self-center"><h6 class="mb-0"><i class="fas fa-euro-sign iconslidermarathon mr-3 "></i> <?php the_field('marathon_price'); ?></h6></div>
<a href="<?php the_permalink(); ?>" target="_blank"><div class="col-2 align-self-center"><h5 class="mb-0 text-center"><i class="fas fa-arrow-right iconslidermarathon mr-3"></i></h6></div></a>
</div>
</div>
<?php
}
?>
<?php
}
?>
<!--end loop-->
Upvotes: 1
Views: 314
Reputation: 346
You're not passing the parameters to WP_Query, just the post_type. Include the array as a parameter to the WP_Query and it will work.
$the_query = new WP_Query( array(
'post_type' => 'kinsta_marathon',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'slider',
'field' => 'slug',
'terms' => 'slider'
)
)
) );
Also, you don't need the if
wrapping the loop. If the list is empty, it won't process the loop and you aren't displaying any sort of message indicating no records if it is empty.
One other change would be to make sure your variables are the same for the query and the loop. You named your query variable $the_query
but you are looping through $queryObject
.
// The Loop!
<?php
while ($the_query->have_posts()) {
$the_query->the_post();
?>
Refer to the WordPress WP_Query page for useful examples.
Upvotes: 2