0
votes

I have a query which is used to display posts on a page when a custom field/meta key contains a phrase (in this case "key = sports" and "value = fishing"). How do I use the query on a single post to display some text when the same criteria is met? The loop query is:

<?php
$args = array(
'meta_query' => array(
    array(
        'key' => 'sports',
        'value' => 'fishing',
        'compare' => 'LIKE'
    ),
));
query_posts($args); while (have_posts()) : the_post(); ?>

I imagine the solution contains a php "if" and "echo" but am rather stumped! I am sorry if the question is unclear - let me know and I will try to better explain. Thank you for your help.

1

1 Answers

0
votes

Inside your loop, you are able to use the $post array for all the objects inside a certain post. Containing your custom values, from there you'll probably want to use if, else statements inside the loop.

Edit: As per your comment, I will include some code for you to use. Instead of plainly displaying the_post();, you can reach all the information inside the post, inside the loop like so. I was also wrong using the $post variable, you can use get_field instead, since you'll be using custom fields.

<?php
$args = array(
'meta_query' => array(
    array(
        'key' => 'sports',
        'value' => 'fishing',
        'compare' => 'LIKE'
    ),
));
query_posts($args); 

while (have_posts()) {
    the_post(); 

    if (get_field('sports') == 'fishing'){
      //Do something
    }
}
?>