Forums

How to query Custom Post Meta and display the result (3 posts)

  1. denzel2364
    Member
    Posted 1 year ago #

    Hi All,

    I was looking at the way Categories displays a list of all the posts under that category and want to replicate it using a custom post meta. For example, display all the posts with a custom value set as 'blue'.

    I have this piece of code which displays the post value:

    <?php $customField = get_post_custom_values("Location");
    						if (isset($customField[0])) {
        					echo "Location: ".$customField[0];
    						}
    						?>
  2. TM3909
    Member
    Posted 1 year ago #

    Wait, so do you just want to echo the Custom Meta in a post? If you want to call the custom value out of a post in the loop, use the following:

    <?php $key="blue"; echo get_post_meta($post->ID, $key, true); ?>

    But if you want to call all the posts with the custom meta "blue", you have to use a query:

    query_posts('meta_key=color&meta_value=blue');

    Where color is the name of your custom field and blue is the value you are looking for.

  3. denzel2364
    Member
    Posted 1 year ago #

    This is perfect. I have the echo set up, just need the query.

    How would i incorporate this into my template as a btn?

Topic Closed

This topic has been closed to new replies.

About this Topic