Support

Account

Home Forums General Issues Show latest custom posts with acf values A, B or C

Solving

Show latest custom posts with acf values A, B or C

  • As per title, I have a custom post type with a bunch of acf fields. Each post has 3 promotional offers: Offer 1, Offer 2 and Offer 3.

    My ACF fields are:
    offer_title_1
    offer_title_2
    offer_title_3

    I’m trying to build a widget which pulls in offer titles from offer 1, 2 or 3 from every post and display them with a link back to the custom post?

    I need to limit them to 4 and either display them randomly or by latest? I’d then like to build a custom page with all current values from offer_title_1, offer_title_2 or offer_title_3 on a page, all with links back to the custom post that has that value/offer.

    Is this even possible?

  • Hi @phil.owen

    I think you should read the WP docs for get_posts. This function will allow you to query the DB for posts.

    Then, you can sort them, loop over them and display any ACF data you wish.

    Thanks
    E

  • Ok great. Kinda have it working, but my code:

    
    <?php
    $posts = get_posts(array(
    	'post_type'		=> 'spot',
    	'posts_per_page'	=> -1,
    	'meta_query'		=> array(
    		'relation' => 'OR',
    		array(
    			'key' => 'offer_title_1',
    			'compare' => '='
    		),
    		array(
    			'key' => 'offer_title_2',
    			'compare' => '='
    		),
    		array(
    			'key' => 'offer_title_3',
    			'compare' => '='
    		)
    	)
    ));
     
    if($posts)
    {
    	foreach($posts as $post)
    	{
    		echo the_title();
    	}
    }
    ?>
    

    is pulling two posts in, one which has one of these meta values and one does not. Trying to work out why it is pulling in one correct and one incorrect.

  • Hi @phil.owen

    Your meta_query arrays are missing the value piece

    Thanks
    E

  • Hi, the values could be anything. I only really want to pull in and list posts with any of the keys present, not by value.

    So each of the 3 offers have a title, description and expiry date and I only want to pull in posts with any titles completed – the value of which will be always changing against the keys and could be anything eg, 2 for 1 on thursdays etc. I just need to check if a value exists for any of the keys and display the post data if it exists.

    Does this make sense?

  • Hi @phil.owen

    Please review the docs here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    You can see that a value does not have to be exact, you can compare EXISTS which I believe will help you out.

    Thanks
    E

  • Thanks Elliot, I tried incorporating this but while it pulls the post in with those values, I also get post name from this:
    At <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    …coming through from another post which doesn’t have any of the values I’m checking for.

    Does this look right or do I need a 'relation' => 'OR', before each array?

    <?php
    $posts = get_posts(array(
    	'post_type' => 'spot',
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'offer_title_1',
    			'compare' => 'EXISTS'
    		),
    		array(
    			'key' => 'offer_title_2',
    			'compare' => 'EXISTS'
    		),
    		array(
    			'key' => 'offer_title_3',
    			'compare' => 'EXISTS'
    		)
    	)
    ));
     
    if($posts)
    {
    	foreach($posts as $post)
    	{ ?>
    
    	<div class="one-third">
    		<?php if(get_field('offer_title_1')) { ?>
    			<a href="<?php echo get_permalink(); ?>"><h3><?php the_field('offer_title_1') ?></h3></a>
    		<?php } ?>
    
    		<?php if(get_field('offer_title_2')) { ?>
    			<a href="<?php echo get_permalink(); ?>"><h3><?php the_field('offer_title_2') ?></h3></a>
    		<?php } ?>
    	
    		<?php if(get_field('offer_title_3')) { ?>
    			<a href="<?php echo get_permalink(); ?>"><h3><?php the_field('offer_title_3') ?></h3></a>
    		<?php } ?>
    	At <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
    	</div>
    	<?php }
    }
    ?>
     

    Thanks for your help with this Elliot. Your plugin rocks!

  • Hi @phil.owen

    You only need the one ‘relation’, this is explained in the WP_Query docs.

    I’m not sure why your query is not working, perhaps the mystery post which is appearing does contain a blank value in the DB containing the custom field.

    Maybe your compare needs to check if the value is not “”, this will allow the custom field to exist in the db, but not be found if it is blank

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Show latest custom posts with acf values A, B or C’ is closed to new replies.