WordPress.org

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] Two-way relations? (15 posts)

  1. raitwebs
    Member
    Posted 3 years ago #

    Let's say I have two custom post types: "Movies" and "Actors"
    I add a new Actor and link it to a Movie. Now when I open up the Actors list in the admin, I see the Movie assigned to the actor. However, if I were to open up the Movies list, there is no Actor assigned to the Movie. I need to open the Movie in question and link it back to the Actor.

    I'm not sure if I'm just not missing an option somewhere...

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 3 years ago #

    No, you are not missing an option. Taxonomies are technically one-way relationships.

    HOWEVER, if desired, you can retrieve the relationship info in both directions, i.e. if your relationship is Movies->Actors but you want to know which movies are tied to an actor, by using the get_objects_in_term() function:

    // first, you need the actor's post id
    $actor_post_id = 321;
    // get_object_in_term() will return term info for all movies who are tied to this actor
    $movies = $cpt_onomy->get_objects_in_term( $actor_post_id, 'movies' );

    I hope this info helps.

  3. probablepossible
    Member
    Posted 3 years ago #

    So-- if I have a list of six hundred authors-- and I want the three thousand quotes to associate with them two ways... do I have to repeat that bit of code six hundred times?

  4. Rachel Carden
    Member
    Plugin Author

    Posted 3 years ago #

    Although I'm hazy on the context of what you're doing, you should never have to repeat code 600 times. You could use a PHP loop to loop through the list of authors with your code inside the loop.

  5. Whysy
    Member
    Posted 2 years ago #

    I have almost the same problem. I have two custom post : Label and Band. On single-band.php , associated labels are well recovered on the page but on the single-label.php I didn't succeed to see the bands for this particular label.

    I tried get_objects_in_term but i have empty array() in result. Any ideas ?

  6. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    Are you using $cpt_onomy->get_objects_in_term()? Some CPT-onomy functions require the CPT-onomy class.

    Don't forget that you'll probably need to add global $cpt_onomy; before you use the variable.

  7. Whysy
    Member
    Posted 2 years ago #

    Yes, i have :

    global $cpt_onomy;
    $terms = $cpt_onomy->get_objects_in_term($post->ID, 'groupe');
     if ($terms && !is_wp_error($terms)) {
    		foreach ($terms as $term) {
    		    echo ("Groupe : " . $term->name);
    		}
    }
  8. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    get_objects_in_term() only returns the IDs for the objects so you have to insert another step to get term/post information.

    I'm about to run out the door but you can check out my get_objects_in_term() documentation for an example.

    If that didn't help, let me know and I'll type up another example when I get a chance.

  9. Whysy
    Member
    Posted 2 years ago #

    Ok thanks for your help, i'll check this out

    Edit : It's ok, thanks again for your help ;)

  10. Whysy
    Member
    Posted 2 years ago #

    In fact another example would help me. For a given label, I can not retrieve the cpt groupe. I have all the groupe even if the label does not match.

    I have :

    <?php
     global $cpt_onomy;
     echo ("<br />Titre : " . $post->post_title . "<br />");
     echo get_the_term_list($post->ID, 'origins', '<p>Origine: ', ',','</p>');
     echo $post->post_content;
     ?>
    
    <?php
     $objects = $cpt_onomy->get_objects_in_term($post->ID,'groupe');
     $args = array(
       'post_type' => 'groupe',
       'post__in' => $objects,
       'orderby' => 'title',
       'order' => 'ASC',
    );
    
    query_posts($args);
     if ( have_posts() ) ?>
     <ul>
     <?php while ( have_posts() ) : the_post(); print_r($post);?>
     <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php the_title(); ?></a></li>
     <?php endwhile; ?>
     </ul>
  11. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    Hey! Sorry for the delayed response. Been a busy week and I haven't been able to check the forum.

    The first thing that jumps out at me is the second group of code. Unless your custom post type 'groupe' is assigned as a CPT-onomy to itself, the post type that you use for get_objects_in_term() should be different than the post type you use for the query posts $args.

    It looks as if you mean for 'origins' to be in get_objects_in_term(). Try this:

    $objects = $cpt_onomy->get_objects_in_term( $post->ID, 'origins' );
    
    $args = array(
       'post_type' => 'groupe',
       'post__in' => $objects,
       'orderby' => 'title',
       'order' => 'ASC',
    );
    
    query_posts( $args );
    if ( have_posts() ) :
       ?><ul><?php
          while ( have_posts() ) : the_post();
             ?><li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php
          endwhile;
       ?></ul><?php
    endif;

    Let me know if this helped... or if you already figured it out =)

  12. Whysy
    Member
    Posted 2 years ago #

    Change nothing, I still have all groups and not only those belonging to a label.

    This is my cpt onomies. I need to display the cpt attached to the 'Label' especially 'Groupes'

    my cpt

  13. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    So you need to display ALL posts that are attached to a label? Just change the 'post_type' to 'any':

    $objects = $cpt_onomy->get_objects_in_term( $post->ID, 'label' );
    
    $args = array(
       'post_type' => 'any',
       'post__in' => $objects,
       'orderby' => 'title',
       'order' => 'ASC',
    );
    
    query_posts( $args );
    if ( have_posts() ) :
       ?><ul><?php
          while ( have_posts() ) : the_post();
             ?><li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li><?php
          endwhile;
       ?></ul><?php
    endif;

    Did that do the trick?

  14. Whysy
    Member
    Posted 2 years ago #

    It works great ! thanks for your help ! =)

  15. Rachel Carden
    Member
    Plugin Author

    Posted 2 years ago #

    Awesome!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic