Support

Account

Home Forums ACF PRO query random post by true and false

Solved

query random post by true and false

  • Hi

    I would like to query posts with the ACF “true and false” option and would like to show one random post. With the code below doesn’t work. It’s always showing the latest post.

    Thanks for your help.

    Joe

    <?php
    query_posts(array(
    	'meta_query' => array(
    		array(
    			'key' => 'post_in_home_gallery',
    			'compare' => '==',
    			'value' => '1',
    			'orderby' => 'rand',
    			'showposts' => 1
    		)
    	)
    ));
    
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    
    <?php echo get_the_post_thumbnail(); ?>
    <div class="home-gallery-title-container">
        <div class="container-wrapper">
    	    <div class="wrapper">
    		    <div class="category-corner-left orange"><?php the_category('cat_name'); ?></div>
    		    <div class="category-corner-right orange">AWG</div>
    		    <div class="clear"></div>
    		    <div class="title"><?php echo the_title(); ?></div>
    	    </div>
    	    <a href="<?php the_permalink() ?>"><input type="button" value="Mehr Informationen" /></a>
        </div>
    </div>
    
    <?php endwhile;
    endif; ?>
    <?php wp_reset_query(); ?>
    
  • the order by needs to be in the main query args and not in your meta query, along with anything else that is not part of the meta_query.

    
    query_posts(array(
    	'orderby' => 'rand',
    	'showposts' => 1
    	'meta_query' => array(
    		array(
    			'key' => 'post_in_home_gallery',
    			'compare' => '==',
    			'value' => '1',
    		)
    	)
    ));
    
  • Aha, I understand! Thank you for the explanation!

    Thanks.

    Joe

    There was another comma:

    query_posts(array(
    	'orderby' => 'rand',
    	'showposts' => 1,
    	'meta_query' => array(
    		array(
    			'key' => 'post_in_home_gallery',
    			'compare' => '==',
    			'value' => '1',
    		)
    	)
    ));
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘query random post by true and false’ is closed to new replies.