Support » Fixing WordPress » Display posts based on custom taxonomy association & custom field meta

  • Hi everyone

    I am working on a site where I have created two custom post types called “tales” and “bios”. These post types have a custom taxonomy associated with them called tale-associations. The tale association taxonomy is hierarchical and is only intended as a means to group individual tales into a Seasons hierarchy and also to associate the people’s bios to the tales.

    What I want to do is on an individual tale page determine the value of the tale-association taxonomy for the tale and use that value to query the bio post types for people that also have the same tale-association value. Next I want to display the returned bios as either cast, or crew or both based on if a custom field has a value.

    The Custom fields come into play because there are going to be 10 tales in Season 1 and many people are working as either “cast” or “crew” or both across multiple tales and will need to have multiple titles associated with them depending on the tale and role. With that in mind I have created 20 custom fields that are associated with the bio page. One field where they can enter in their character’s name for the cast role per tale, and a field for each crew role per tale. This means that they only have to maintain one bio page but we can dynamically display their role/title depending on the query we set up.

    I know that may sound a bit complicated so I will give you an example of how I have this set up using a real example.

    I have created a tale called “Man on the Ledge”. I have associated this tale in the tale-association taxonomy with both ‘Season-1’ and “tale-man-on-the-ledge”. Next I have created bios of several people, some of whom have both a cast role and a crew role in the tale. These bios have all been associated with “tale-man-on-the-ledge” within the custom tale-association taxonomy I have created.

    So to continue with the example above the people who have cast roles have text added to the the custom field called “man-on-the-ledge-role”. Those that have a crew role have text entered into the custom field called “man-on-the-ledge-crew-role”.

    Now admittedly I am not a PHP expert by any means, but I have been at this for several evenings trying to make it work for me and I’m lost. I know logically it should work I just can’t seem to come up with the proper query string to get the info I need.

    This is what I have so far:

    <?php
        query_posts( array(
            'tale-association' => 'tale-man-on-the-ledge',
            'showposts' => 100,
            'post_type' => 'bios',
            'order'    => 'ASC'
             ) );
            if(have_posts()){
                while ( have_posts() ) : the_post();  ?>
    
                <div id="post-<?php the_ID(); ?>" class="related-post">
                    <?php the_title();?>
    
                </div>
                <?php endwhile; wp_reset_query();
            }
    ?>

    Now this does return all the bios associated with ‘tale-man-on-the-ledge’ but I need to be able to pull that value in dynamically based on the association on the actual tale page. I am assuming I have to create one query for the cast roles and one for the crew roles. This is what it should do if I’m not mistaken:

    – What tale-association does this tale have
    – Take that tale-association and use it to query all the bio posts and return the ones with the same tale-association
    – If the tale-association for the bio page is “tale-man-on-the-ledge” and I want to display only the cast members, then only display bios that have a value for the custom field of “man-on-the-ledge-role”
    – Display the title of the bio pages and then the value of the “man-on-the-ledge-role” custom field.

    If anyone can help shed some light on what I can do to try and make this work I would really appreciate it. I’ll continue working on it and if I come up with a solution I will certainly post for someone else who may have a similar issue.

Viewing 1 replies (of 1 total)
  • Thread Starter brainlava

    (@brainlava)

    Just wanted to say I was able to get some of the issue working by following the example in this thread:

    I added this to my functions.php file:

    // Calling custom tale-association taxonomy
    
    function has_taleAssociation( $taleAssociation, $_post = null ) {
        if ( empty( $taleAssociation ) )
            return false;
    
        if ( $_post )
            $_post = get_post( $_post );
        else
            $_post =& $GLOBALS['post'];
    
        if ( !$_post )
            return false;
    
        $r = is_object_in_term( $_post->ID, 'tale-association', $taleAssociation );
    
        if ( is_wp_error( $r ) )
            return false;
    
        return $r;
    }

    And then in my code I referenced it this way:

    <?php if ( has_taleAssociation( 'tale-man-on-the-ledge' ) ) {
        query_posts( array(
            'tale-association' => 'tale-man-on-the-ledge',
            'showposts' => 100,
            'post_type' => 'bios',
            'order'    => 'ASC'
             ) );}
        elseif ( has_taleAssociation( 'tale-conformation' ) ) {
        query_posts( array(
            'tale-association' => 'tale-conformation',
            'showposts' => 100,
            'post_type' => 'bios',
            'order'    => 'ASC'
             ) );}
    ?>
    
    <?php while ( have_posts() ) : the_post() ?>
        <div id="post-<?php the_ID(); ?>">
            <a href="<?php the_permalink() ?>"><?php the_title();?></a>
        </div>
    <?php endwhile; ?>

    I figure I will continue the elseif / else statements for all 10 tales. But I am still working out how to filter the posts for crew vs cast by the custom field meta data associated with the bios.

Viewing 1 replies (of 1 total)
  • The topic ‘Display posts based on custom taxonomy association & custom field meta’ is closed to new replies.