WordPress.org

Ready to get started?Download WordPress

Forums

Custom post types and relations (28 posts)

  1. niska
    Member
    Posted 3 years ago #

    s it possible to create a relation between a custom post type and a regular post?

    Let's say that my post is about a movie that I have defined as a custom post type. How do i make such a relation in WP 3.0?

  2. MichaelH
    Member
    Posted 3 years ago #

    Related it in what sense? In this case would wonder why you even need a custom post type.

  3. niska
    Member
    Posted 3 years ago #

    I have several authors that write reviews about movies. There can be several reviews (posts) about a movie. I would like to make the movie as a custom post type. Each movie should have a title, description and a EAN-number.

    When looking at a movie I would like to link to all the reviews (posts) of that movie.
    When looking at a review (post) I would like to link to the movieinformation-page.

    Is this possible in WordPress 3.0? And how would I go about it to make it work?

  4. niska
    Member
    Posted 3 years ago #

    My only problem is how to create a relation between custom posts.

    I've actually found two plugins for this:

    http://wordpress.org/extend/plugins/custom-post-relationships/
    I've tried this plugin, and defined the relation between a movie and a post. But I'm not sure how to edit the template, so that there is links from a movie to the posts and from the posts to the movie.

    http://wordpress.org/extend/plugins/custom-post-relationships/
    This plugin can only (when writing this) only create relations between posts. It seems as it will be updated in the future to also be able to handle custom posts.

  5. niska
    Member
    Posted 3 years ago #

  6. fusionserve
    Member
    Posted 3 years ago #

    I'm having a problem with this plugin using the widget, it outputs unrelated data.

    Is there another way to output related data?

  7. scribu
    Member
    Posted 3 years ago #

    I also have a plugin that supports connections between all post types (including attachments):

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

  8. probablepossible
    Member
    Posted 3 years ago #

    scribu, I just installed your plugin-- but i cannot find a meta box in edit-post, or a link in the TOC or anything!

  9. scribu
    Member
    Posted 3 years ago #

    You need to register a connection type. An example is right on the plugin page.

  10. probablepossible
    Member
    Posted 3 years ago #

    Ah-- via the plugins editor, huh?
    Okay-- I have a very limited knowledge of php coding. I can copy and paste your example and I know what to change for my purposes. But in which file do I put it? You don't say, and I've looked though your files and I am not experienced enough to know by their contents.

  11. scribu
    Member
    Posted 3 years ago #

    Ah, sorry about that. Just put in your theme's functions.php file.

  12. stevebluck
    Member
    Posted 3 years ago #

    You could create a custom write panel with a dropdown box with all movies listed inside it. This way you could create a relation between the two and have them do whatever.

    You will need someone with coding knowledge though.

  13. Cristiano
    Member
    Posted 3 years ago #

    Hey Scribu! Thanks for your amazing plugin!

    I got to make the connection, but I have no idea how to query it...
    Exemple: I have two related post types called "review" and "movie". I created the relationship between both. ok.

    Now I want a query like: On the Movie page, I want to show all reviews related to that movie. What should I do?

    Thanks!!!!

  14. scribu
    Member
    Posted 3 years ago #

    Since you're on 04-beta, you can do this:

    global $post;
    
    query_posts( array(
      'post_type' => 'review',
      'connected' => $post->ID,
    ) );
  15. probablepossible
    Member
    Posted 3 years ago #

    Scribu, would that effectively become a table of contents?

    If the post type is arranged hierachally, would the list be in the ordered format?

  16. scribu
    Member
    Posted 3 years ago #

    Scribu, would that effectively become a table of contents?

    No, you still have to write the loop to display the found posts.

    If the post type is arranged hierachally, would the list be in the ordered format?

    Don't know. See the Codex:

    http://codex.wordpress.org/Function_Reference/query_posts#Orderby_Parameters

  17. Cristiano
    Member
    Posted 3 years ago #

    Thanks for your answer Scribu!

    I´m sorry, I´m a really begginer on this... so, could you tell me how is the whole query/loop to get the connected posts?

    I wrote this, but it doesn´t work:

    <?php
      global $post;
      $the_query = query_posts( array(
      'post_type' => 'review',
      'connected' => $post->ID,
       ) );
    
      while ($the_query->have_posts()) : $the_query->the_post();
      $do_not_duplicate = $post->ID;
      $type = get_post_type();
    ?>

    Thanks for your help!

  18. Cristiano
    Member
    Posted 3 years ago #

    Ok, sorry... I got it!

    Loop inside the main loop:

    <?php
      global $post;
      query_posts( array(
         'post_type' => 'review',
         'connected' => $post->ID,
      ) );
    
      if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      ....
      <?php endwhile; endif; ?>

    ;)

  19. scribu
    Member
    Posted 3 years ago #

    Great!

    However, that might cause problems later in the page.

    You should use a different loop object, like in your first attempt:

    global $post;
    
    $the_query = new WP_Query( array(
      'post_type' => 'review',
      'connected' => $post->ID,
    ) );
    
    while ($the_query->have_posts()) : $the_query->the_post();
      ...
    endwhile;
    
    wp_reset_postdata();
  20. Cristiano
    Member
    Posted 3 years ago #

    Ok, so if I already used $the_query in my first loop, now I should use some diferent, like $the_query2 or whatelse, right?

    Thanks Scribu!

  21. scribu
    Member
    Posted 3 years ago #

    Yep.

  22. bluebird2
    Member
    Posted 3 years ago #

    I am trying to show the author(s) for the book on my site's sidebar using this plugin. I have checked the writer's name in the books page (in the admin) but the widget still shows the whole list of the writers instead of showing only the related author. How can I fix that?

  23. scribu
    Member
    Posted 3 years ago #

    This plugin doesn't provide any widgets, so what are you using to display the writers?

  24. bluebird2
    Member
    Posted 3 years ago #

    I am sorry. I was talking about this plugin:
    http://wordpress.org/extend/plugins/relation-post-types/

  25. markzero
    Member
    Posted 3 years ago #

    Is there a way to define meta for the relationship between two post types?
    Like the product-store example:
    product - 20eur - store1
    product - 22eur - store2

  26. scribu
    Member
    Posted 3 years ago #

  27. bluebird2
    Member
    Posted 3 years ago #

    scribu, why don't you add widgets and admin setting options to this plugin. Apparently yours is the most advanced plugin for this purpose but needs a lot of PHP knowledge to use it.

  28. scribu
    Member
    Posted 2 years ago #

    Because widgets would either be very limited or be very heavy, option wise.

Topic Closed

This topic has been closed to new replies.

About this Topic