1

I'have this structure:

  • News (CPT. A page showing all news)
  • Works (CPT. A page showing all works)

Then, inside a single page of works I want to show the news related to this work:

  • Single work
    • title
    • content
    • news related to this single work

How can I make a relation between a single post in News and single post in Works?

AFAIK, I can create a category term with same name of each single work, by hand, and then to assign that term to News single post. But there is high risk of typos and a lot of work in the content editor. Is it possible to get a more automated method?

Thank you.

2
  • Tags are your best friends here. That's one of the reasons they exist.
    – Jack Johansson
    Jun 13 '17 at 13:24
  • Thanks for the answer. So, I should to create, by hand, in each single News post, a tag for each work post, with the same name of each Work post and then, to do a query inside single work template using the work title as tag criterion. Is it?
    – aitor
    Jun 13 '17 at 13:31
1

You have to set the relations manually. WordPress is not smart enough to relate to posts by tags if you don't set them yourself ( It can however use categories, but not really efficient ).

What you can do, is to set some tags on a News post, and then use them to search the tags in Works posts. Here is an example:

get_related_posts($id){
    // Get current post's tags
    $tags = wp_get_post_tags($id);
    // Check if the post has any tags
    if ($tags) {
        // If it does, get them and make a query based on tags
        $tag_ids = array();
        foreach($tags as $individual_tag) {
            $tag_ids[] = $individual_tag->term_id;
        }
        $args = array(
            'tag__in' => $tag_ids,
            'post__not_in' => array($id),
            'posts_per_page'=>8, 
            'ignore_sticky_posts'=> 1,
            'post_type' => 'works'
        );
        $related_query = new WP_Query( $args );
        // Starting the loop
        if ( $related_query->have_posts()) {
            while ($related_query->have_posts()){
            $related_query->the_post();
                the_title();
            }
        }
    }
    wp_reset_postdata();
}

Feed this function with a post ID and you will get a list of related posts in the Works post type that have the same tags as this post:

get_related_posts(get_the_ID());

You can do a conditional to use the same function for both post types.

0
1

You can use Advance custom fields plugin for this. Select a field group for CPT works and use field type "Relationship" with CPT news. It will provide UI in the admin panel to select one from available news.

2
  • Let me know if you need any help on this from me. Accept the answer if helps you to achieve your goal. Jun 13 '17 at 15:13
  • Sorry, relational field needs a pro license for the plugin :( Jack's answer works without plugin and I can't accept two answers. Nex time! :)
    – aitor
    Jun 13 '17 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.