Support » Fixing WordPress » Template page with get post by "Tag"?

  • Resolved shawncbarry


    I’m re-building an HTML/CSS static site into a WordPress custom theme. This is ambitious because I only know HTML and CSS. I couldn’t write a full PHP tag that functioned correctly to save my life – just for reference. I’ve done this by following a Lynda tutorial. So far, everything’s good.

    To be clear, I’m not looking for “free development help”, but if someone could kindly let me know what kind of approach I might take, I’ll happily do the PHP research myself to find the actual code. Here’s what I want to accomplish…

    I have a site that features different people with a personal bio page and several posts they’re in. The bio page uses a template that gets information dynamically (name, age, etc) by using Custom Fields and get post meta, like this:

    <h1><?php echo get_post_meta($post->ID, 'name', true); ?></h1><br /><br />
    			<p><?php echo get_post_meta($post->ID, 'name', true); ?>

    That works fine. What I’d like to do now, is pull in every post they’re in by “tag”. In other words, if you clicked on “Sally”, her bio page would show the templated meta (above), and below that, it would say “Here are the posts involving Sally”. Looking for what kind of approach I need to create a loop that looks for a post tag of Sally, and echos whatever number of posts there are to her bio page (could be one, could be five). The kick – this bio page is used for other people as well, so it needs to be variable.

    I have a current “messy” solution, but it’s not dynamic. Is this possible, or do I need to set up a unique template for each person?

    Help is appreciated. I can give a link, but with respect, I’m an adult content developer so I’ll wait to see if that’s necessary.

Viewing 6 replies - 1 through 6 (of 6 total)
  • This will be a lot easier if you use a taxonomy instead of custom fields — custom fields really aren’t designed to work across different posts without extra of work; they’re meant to give some extra structure to the data in that particular post. Taxonomies are intended to connect different posts in exactly the way you describe.

    You have two built-in taxonomies available, Categories and Tags, and you can add as many custom taxonomies as you like; if you use any of those, it’s just a matter of going to (or, if that’s the custom tax you set up). And of course you can style the template however you like, but that’s a much simpler problem than hand-building a page for each item in your taxonomy!

    Thank you Amy. Looking into this now. I found some code that just returns every post made on the site, so I just have to figure out how to call the tags properly. This was provided by another WP member, MichaelH:

    //for in the loop, display all "content", regardless of post_type,
    //that have the same custom taxonomy (e.g. genre) terms as the current post
    $backup = $post;  // backup the current object
    $found_none = '<h2>No related posts found!</h2>';
    $taxonomy = 'genre';//  e.g. post_tag, category, custom taxonomy
    $param_type = 'genre'; //  e.g. tag__in, category__in, but genre__in will NOT work
    $post_types = get_post_types( array('public' => true), 'names' );
    $tax_args=array('orderby' => 'none');
    $tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
    if ($tags) {
    foreach ($tags as $tag) {
    "$param_type" => $tag->slug,
    'post__not_in' => array($post->ID),
    'post_type' => $post_types,
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    <?php $found_none = '';
    if ($found_none) {
    echo $found_none;
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again

    The one thing I can’t seem to get my head around is how do I tell the above code in the page template to get a category for “person 1”, then “person 2” since their bio pages are using the same template? Seems I have to say “This page is related to posts with this category” but the page doesn’t have a category application.

    Update – found this, and it works. Thanks again to MichaelH.

    Excuse all the updates, but I feel like I’m chasing a rabbit that I grab, but it slips out of my fingers and keeps running just ahead of me.

    In the code on Amit’s page, and with the code above, I’m STILL unable to tell the template what categories to display on what page. I’m now getting every post that has a category. But I want only posts that match the category of the page.


Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Template page with get post by "Tag"?’ is closed to new replies.