Support

Account

Home Forums General Issues Need help placing get_field inside an array

Solved

Need help placing get_field inside an array

  • I am attempting to display blog posts based on tags. The base code was initially:

    <?php $the_query = new WP_Query( array( ‘tag’ => ‘great-expressions’,’posts_per_page’ => 5 ) ); ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <p>“><?php the_title(); ?></p>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    I created a custom field where you can input the tag name:

    blog_post_tags

    I’m not sure how to add the get_field function to the array for ‘tag’ – so that it will pull the text from the custom field.

    <?php $the_query = new WP_Query( array( ‘tag’ => ‘great-expressions’,’posts_per_page’ => 5 ) ); ?>

    I’m a novice to this kind of thing, so please excuse my naivete. Any help anyone can offer would be greatly appreciated. Thanks, in advance!

  • Hi @greghoffman

    You can store the field value in a variable and then pass this variable in your query like so:

    <?php 
    $value = get_field('field_name');
    $the_query = new WP_Query( 
    array( 
    ‘tag’ => $value,
    ’posts_per_page’ => 5 
    ) 
    ); 
    ?>
  • Thank you so much! That worked perfectly!

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

You must be logged in to reply to this topic.