0

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>
1

I think you're making this more difficult that it should be,

If I understand correctly you want to display the latest 4 'videos' which have been flagged as 'our work' in your first column and those flagged as 'featured film' in the next column, so you should actually add that custom field parameter to your query,

$args = array(
  'post_type' => 'videos',
  'posts_per_page' => 4 ,
  'meta_key' => 'labeled_as',
  'meta_value' => 'our work'
);
$loop = new WP_Query( $args);
while ( $loop->have_posts() ) { 
  //display your work videos
}
$args = array(
  'post_type' => 'videos',
  'posts_per_page' => 4 ,
  'meta_key' => 'labeled_as',
  'meta_value' => 'featured film'
);
$loop = new WP_Query( $args);
while ( $loop->have_posts() ) { 
  //display your featured videos
}
/* Restore original Post Data */
wp_reset_postdata();

Notes:

  1. Reset your postdata (last function call in the above example) this is important to ensure WP request continues as normal with the rest of your page.
  2. Instead of using a custom field 'labelled_as' for your organising your 'videos' I would recommend you use a custom taxonomy, this would allow you to leverage a lot more WP functionality at a later stage of your site development!
  • thanks! I'll look into the custom taxonomy as well. – jessica mele May 21 '18 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.