Support

Account

Home Forums General Issues Get the page that have particular value in advanced custom field

Solving

Get the page that have particular value in advanced custom field

  • Hi,

    All my pages have advanced custom field called : “Program slug”

    I would like to get link to the page that have particular value in that custom field.

    For example :
    Want to get link to the page that has custom field “Program slug” set to “building-speed”

    This code will work?

    <?php 
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'page',
    	'meta_key'		=> 'program_slug',
    	'meta_value'	=> 'building-speed'
    );
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>">
    				<img src="<?php the_field('event_thumbnail'); ?>" />
    				<?php the_title(); ?>
    			</a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();	
  • Hi @martin

    Yes, your code will work for simple text based ACF fields.

    For complex fields, the code will be a little different.

    Check out this tutorial for more information: http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

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

The topic ‘Get the page that have particular value in advanced custom field’ is closed to new replies.