WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Posts 2 Posts] function to display related posts on frontend? (9 posts)

  1. Anointed
    Member
    Posted 4 years ago #

    I have to say the backend is working perfectly. Where I am getting stuck is trying to figure out the 'display' function on the front end.

    scenario:
    2 post types, movies, actors related together in backend

    What would be simple function to display the actor that is 'related' to the book being displayed. I know how to display post type meta information already, but can't seem to figure out how to display the post type meta information that is 'related' to the original post type.

    also have 2 other quick questions.
    1. How to assign an array of objects to a type.
    -- say there were multiple actors in the movie, and I want to display them all. yeah, I know, bad example but I think it gets the point across

    2. How to assign an array of post types to another type.
    -- say I want to not only have actors type assigned to the movie, but directors as well.

    finally:
    most of the time, I am not simply going to display the actor name on the movie page, but also the actor's picture and profile info.

    I have no problem creating an actors post type, with the input for name, image, description.

    Still trying to figure out how to display the meta array of information for each actor assigned to the movie on the front end.

    p.s.
    It would be really great someday to see a generic movie theme with notations in the code so we all could follow along and learn how to do this.
    While I have no need for a 'movies' setup, I believe it would cover all the bases for what most people are after, as you would have fields:

    1. movie post type
    -- production date
    -- cover image
    -- description
    -- gallery images

    2. actors post type
    -- actors name
    -- actors image
    -- actors description

    Obviously a lot more could be added, but this would give great 'training' on how to relate multiple post types, and how to display both single related meta fields and arrays of meta fields on the front end.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Posted 4 years ago #

    1. How to assign an array of objects to a type.
    -- say there were multiple actors in the movie, and I want to display them all. yeah, I know, bad example but I think it gets the point across

    See this topic: [Plugin: Posts 2 Posts] More than one related post.

    2. How to assign an array of post types to another type.
    -- say I want to not only have actors type assigned to the movie, but directors as well.

    p2p_register_connection_type('movie', 'actor');
    p2p_register_connection_type('movie', 'director');

    I will probably make it accept an array as the second argument too.

    Displaying conencted posts:

    The most flexible way to display related posts is through a custom loop:

    $connected_posts_ids = p2p_get_connected('to', get_the_ID(), 'actor');
    
    query_posts(array('post__in' => $connected_posts_ids));
    while ( have_posts() ) : the_post();
    
        the_title();
    
        // etc.
    
    endwhile;
    wp_reset_query();

    The first argument is either 'to' or 'from'.

  3. Anointed
    Member
    Posted 4 years ago #

    I was not getting anything returned using a 'to' argument, when querying actor from the movies single page. I do have an actor chosen in the 'connections' dropdown, so that part seems to work.
    When I run a print_r of $connected_posts_ids I get the following returned:

    Array ( [0] => 78 )

    must be missing something simple, as the function seems correct.

  4. scribu
    Member
    Posted 4 years ago #

    Yes, it's supposed to return an array of related post ids. You then use that id list in query_posts(), as demonstrated above.

  5. Anointed
    Member
    Posted 4 years ago #

    This one has me stumped, here is the code I am trying:

    <?php
    $connected_posts_ids = p2p_get_connected('from', get_the_ID(), 'artists');
    print_r($connected_posts_ids);
    query_posts(array('post__in' => $connected_posts_ids));
    $testing = query_posts(array('post__in' => $connected_posts_ids));
    print_r($testing);
    while ( have_posts() ) : the_post();
    echo get_post_meta(get_the_ID(), 'artistsimage', true);
        the_title();
    
        // etc.
    
    endwhile;
    wp_reset_query();
    ?>

    the first part works:
    print_r($connected_posts_ids);
    That returns Array ( [0] => 78 )

    However
    print_r($testing);
    returns Array ()
    That is obviously the root of my problem as to why my artistsimage and title are not showing up.

    What I don't understand is where the array is 'loosing' it's information between the first line and the second line of code.

  6. Anointed
    Member
    Posted 4 years ago #

    huge props to a friend who figured this out for me. Posting here, because I am SURE that others will have this issue someday.

    <?php
    $connected_posts_ids = p2p_get_connected('from', get_the_ID(), 'artists');
    query_posts(array('post__in' => $connected_posts_ids,'post_type'=>'artists'));
    while ( have_posts() ) : the_post();
        the_title();
    ?>
    <img src="<?php echo get_post_meta(get_the_ID(), 'artistsimage', true);?>">
    <?php
        // etc.
    
    endwhile;
    wp_reset_query();
    ?>

    the KEY was changing this one line:
    $connected_posts_ids,'post_type'=>'artists'));

    notice the post_type in there... otherwise it returns an empty set.

    sure hope this helps someone else out.

  7. scribu
    Member
    Posted 4 years ago #

    With the development version (0.2-alpha), you can do this:

    function my_p2p_callback($query) {
    	while ( $query->have_posts() ) : $query->the_post();
    		the_title();
    
    		// etc.
    	endwhile;
    }
    
    ... later ...
    
    p2p_list_connected('artist', 'to', get_the_ID(), 'my_p2p_callback');

    p2p_list_connected() takes care of getting the correct posts and sends a WP_Query object to the callback.

    To simply output an unordered list of post titles, do:

    p2p_list_connected('artist', 'to');

  8. Anointed
    Member
    Posted 4 years ago #

    Now that is so nice to have. After playing with post-to-post all day yesterday, I can see how this is going to become one of the favorite development plugins out there. Add in the taxonomy-metadata plugin, and I'm having a hard time finding any missing pieces for my projects.

    Thank you very much!

  9. sara cannon
    Member
    Posted 3 years ago #

    Just thought I'd list generically what I did with the latest version of the plug-in after wandering around and looking at what other people have done. Thought I'd post in-case anyone else wanders upon this thread like I have...

    global $post;
    
    $sc_query = new WP_Query( array(
      'suppress_filters' => false,
      'post_type' => 'artist',
      'connected' => $post->ID) );
    
    if($sc_query->have_posts()) :
    
    echo '<ul>';
    
    while($sc_query->have_posts()):$sc_query->the_post();
    $sc_output ='';
    $sc_output .= '<li><a href="' . get_permalink($post->ID) . '">';
    $sc_output .= $post->post_title;
    $sc_output .= '</a></li>';
    
    echo $sc_output;
    
    endwhile;
    
    echo '</ul>';
    
    endif;

Topic Closed

This topic has been closed to new replies.

About this Topic