I have a custom post type called 'videos' and I am using the Advanced Custom Fields plugin with a select field to use as a filter to assign where the post part will display on my page. I have two columns, one called "our work' and one called 'featured films'. I need each section to display the latest 4 posts but when I change posts_per_page it affects the total number, is there a way to limit it to just 4 per query? Sub question is it ok to have the same query run two times? My code is :
<div class="triple ourWork col-sm-6">
<h2>Our Work</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page'
=> 4) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>
<?php if( get_field('labeled_as') == 'our work' ): ?>
<div class="col-sm-6" id="">
<?php global $post;
$gethref = $post->post_name;
?>
<div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><a href="/<?php echo $gethref ?>"><span class="play"><?php the_title(); ?></span></a></div>
<p><?php echo the_field('issue_short_description'); ?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<div class="triple featuredFilms col-sm-6">
<h2>Featured Films</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page' => 4 ) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>
<?php if( get_field('labeled_as') == 'featured film' ): ?>
<div class="col-sm-6" id="">
<?php global $post;
$gethref = $post->post_name;
?>
<div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><a href="/<?php echo $gethref ?>"><span class="play"><?php the_title(); ?></span></a></div>
<p><?php echo the_field('issue_short_description'); ?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
You want to use meta queries for acf fields. https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I personally would use 2 variables for the 2 loops for debugging, etc.
$work_loop = new WP_Query(
array(
'post_type' => 'videos',
'posts_per_page' => 4,
'meta_key' => 'labeled_as',
'meta_value' => 'our work'
)
);
This will fetch only posts that have that label. The way you are doing it currently, you get 4 results then filter for the label. So if 3 are in the other label, you will only show 1 result. This gets you 4 that will show. Just change the meta value for the second loop and you should be good to go.