Support

Account

Home Forums Front-end Issues If this ACF field is True, then show 5 posts

If this ACF field is True, then show 5 posts

  • I’m new to dealing with ACF & WordPress, so my apologies for any boneheadedness which may be present here.

    Trying to find the easiest way to do the following:

    * Check the category “case-studies” (id=224)
    * See whether the ACF True/False field “hp_slider” is “true”
    * Show the first 5 posts that match these criteria, and format with the specific values

    My big issue is that I can get WP to show either the first of such posts, or to show all posts, regardless of whether or not it has the “hp_slider” field set to true or false.

    here is the code I’m using, and I realize that it may be woefully wrong & convoluted. Any help is immensely appreciated:
    <?php if(get_field('hp_slider') == true) : ?>
    <?php query_posts('cat=224&showposts=5&order=DESC&orderby=post_date'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    Here is some html/css…

    <?php endwhile; endif; ?>
    <?php endif; ?>

  • This is how I got it working, if this is a terrible solution, please, let me know 🙂

    <?php 
    			 
    // args
    $args = array(
    	'numberposts' => 1,
    	'meta_key' => 'hp_slider',
    	'meta_type' => 'BINARY',
    	'meta_value' => "1"
    );
    			 
    // get results
    $the_query = new WP_Query( $args );
    		 
    // The Loop
    ?>
    <?php if( $the_query->have_posts() ): ?>
    
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        HTML CODE
    	<?php endwhile; ?>
    <?php endif; ?>
  • That should only give you 1 post?

    You can also do this:

    
    
    $args = array(
    	'numberposts' => 5,
    	'meta_key' => 'hp_slider',
    	'meta_compare' => '=',
    	'meta_value' => "1"
    );
    
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘If this ACF field is True, then show 5 posts’ is closed to new replies.