WordPress.org

Ready to get started?Download WordPress

Forums

Include value of custom field in query_posts (2 posts)

  1. adam533
    Member
    Posted 4 years ago #

    I am not sure if this can be done but am hopeful it can be :)

    I am working on a very complicated layout using multiple custom post types and taxonomies. I need to create many different templates so only certain posts are displayed on certain pages. My thinking was I could use one template and then use custom fields to popular the query_posts info.

    What I am trying to do (with no success thus far) is the following

    1. Create a page called Goalie
    2. Create a page template called Drills
    3. On the page Goalie, add a custom field titled age-group with a value of 6
    4. In the page template, use query_posts. I want to be able to take the age-group=6 and add it to the query post automatically.
    5. This way I can create multiple pages like Goalie (ex. Dribbling, Agility, etc) assign them all the page template of Drills and popular the query_posts on the drills template by using the custom field

    The actual layout and coding will be more complex than that (much more!) but I wanted to simplify it for this question. So is there anyway to assign a value to a query_post from a custom field on a page?

    I hope I made sense with this :)

  2. vtxyzzy
    Member
    Posted 4 years ago #

    It certainly is possible to use a custom field value from a page in the query_posts in the template for the page. It is hard to give a specific answer because you did not tell exactly how you wanted to use 'age-group' in the query. Here is how to get the value(s):

    $age_group = get_post_meta($post->ID,'age-group',true);

    If you have several fields, it will be more efficient to use this:

    $meta = get_post_custom($post->ID);
    $age_group = $meta['age-group'][0];
    $skill_level = $meta['skill-level'][0];

    Be sure to test for missing values and take a default value or action.

Topic Closed

This topic has been closed to new replies.

About this Topic