0
votes

So I'm trying to achieve this:

I've created custom post types and there is one custom post type where I want variable sections in. That post type is "workarea". There are four posts (main pages) for that custom post type.

At the bottom of those pages, there is a related posts section.

Each of those, should show different related posts. But, I use the "single-workarea.php" as the custom post type template. So there is no way of (at least that I know of) to create a loop that shows the different related posts, that should be displayed per page.

If I edit that loop, it applies to (obviously) all pages from the custom post type "workarea".

Is there a way to create a variable/custom section in that post type php file?

A solution is that I remove that post type, and just create pages with template names, such as "workarea", but the problem with that is, is whenever my customer wants to add a new post type "workarea", he has to come to me so I can create a new php file.

I hope I made a clear explanation.

Thank you in advance!

2
At the bottom of the template that you use to show single 'werkvelden' posts, create a standard wp query loop and list them that way? - GBWDev
@GBWDev Hi, sorry, I've forgotten to edit it back to the English like term "workarea". The idea is that the four post in the post type "workarea" all have different related posts at the bottom. - user9355172
Ok, so all you have to do is create a standard wp query loop in the bottom of your single-workarea.php file and output the results. Your search query terms for the loop would just be of the same post type - GBWDev
The problem with that is, is that all custom post types from "workarea", show all the same related posts. There are four, and all four should show their own related posts. If I apply a query there, right now, it will show all the same posts on all four pages, which is not the idea. - user9355172
When you say 'related posts' - what counts as a related post? What controls whether a post is related or not? Are these related posts of the same post-type ? e.g. are they also a post type of 'workarea' ? - GBWDev

2 Answers

0
votes

The answer is a work in progress based on what we talked about in the comments section. We'll have to continually improve it until we reached the desired outcome.

As described in the comments section, we should be able to achieve this with a custom loop.

If you check the WP_Query docs, it will show you all the ways you can filter the search by for the $args array

single-workarea.php

<?php 
    // get current post category,
    // assumes there is only ONE category assigned to the current post

    $category = get_the_category( get_the_ID() ); 

    $args = array(        
        'posts_per_page' => -1, // get all posts
        'orderby' => 'random', // randomly get posts
        'post_type' => ['food', 'staff', 'themes'], // search only these post types
        'post_status' => 'published', // only get published posts
        'cat' => $category[0]->term_id // search posts only with this category ID
     );
    $the_query = new WP_Query( $args );

    while ( $the_query->have_posts() ) {
      $the_query->the_post();
      //echo related post data...
      echo '<a href="'. get_permalink() .'">' . get_the_title() . '</a>';
    }

    wp_reset_postdata();
?>

Based on the above, you should be able to get what you need, or close to it.

0
votes

So I've solved this problem with the Advanced custom fields taxonomy field.

This is the loop now:

<?php
        $relevant = get_field('relevante_posts_categorie');

        $postid = get_the_ID();
        $loop = new WP_Query( array(
            'post_type' => 'actueel',
            'posts_per_page' => 3,
            'category__in' => $relevant,
            'post__not_in'=> array($postid),
          )
        );
        ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <a href="<?php the_permalink();?>">
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 relevant__item">
              <div class="thumbnail__wrapper">
                <div class="thumbnail" style="background-image:url('<?php the_field('thumbnail');?>');"></div>
              </div>
              <div class="title">
                <h4><?php the_title();?></h4>
              </div>
            </div>
          </a>
        <?php endwhile; wp_reset_query(); ?>

The taxonomy field get's the category I select. Now it's customizable which posts I want to display. The only thing left to do is create multiple query loops where I define the custom post types.

Thank you for your help.