WordPress.org

Forums

[resolved] How to insert page title into query_posts with custom fields (4 posts)

  1. paulbyB
    Member
    Posted 4 years ago #

    I am trying to work out how to create the code on my page.php template so that when I open a page (eg with the title 'Spain'), all posts that have a custom field with a key of 'country' and the value of 'Spain' will show.

    I have figured out that I need to insert:
    <?php query_posts('meta_key=Country&meta_value=Spain'); ?>

    ahead of the loop. But what I want to be able to do is to dynamically insert the name of the page into the part of the code where 'Spain' is.
    I will have many countries and I don't want to have to create an if/then statement for every country.

    Is this possible?

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    try

    <?php query_posts('meta_key=Country&meta_value=' . $post->post_title); ?>
  3. paulbyB
    Member
    Posted 4 years ago #

    ah, thankyou VERY much!!

    I got really close to this through bits of research but you have finished it off for me.

    that's great, it actually opens up quite a bit of stuff for me.

  4. alchymyth
    Forum Moderator
    Posted 4 years ago #

    a bit of research is never wrong ;-)

    if this is sorted, please mark the thread as 'resolved' - thanks

Topic Closed

This topic has been closed to new replies.

About this Topic