Skip to content

Instantly share code, notes, and snippets.

@wpflames
Last active March 23, 2022 19:02
Show Gist options
  • Select an option

  • Save wpflames/b61632b221eac8083c77f194a7548707 to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/b61632b221eac8083c77f194a7548707 to your computer and use it in GitHub Desktop.
Query WordPress Posts by Advanced Custom Fields
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'YOUR_POST_TYPE',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'YOUR_ACF_SLUG',
'value' => 'YOUR_ACF_VALUE',
'compare' => 'LIKE'
)
)
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h3><?php the_title(); ?> </h3>
<p><?php the_field('YOUR_ACF_SLUG'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment