Stojko
Stojko

Reputation: 1

Custom post type loop the parent category

On single.php i have a standard WP loop which displays post content.

<main>
    <div class="container">
        <?php if (have_posts()): while (have_posts()) : the_post(); ?>

            <article>
                <div class="col-md-12">
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                </div>              
            </article>
        <?php endwhile; ?>
        <?php endif; ?>
    </div>
</main>

Bellow it i have simple loop that displays 3 latest custom post type items

<div class="manuals">

            <?php
                    $loop = new WP_Query( array(
                            'post_type' => 'manuals',
                            'posts_per_page' => 3
                        )
                    );
            ?>

            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                    <div class="col-md-12">

                                <?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                        <?php the_post_thumbnail(); // Fullsize image for the single post ?>
                                    </a>
                                <?php endif; ?>         
                                <h5><?php the_title(); ?></h5>
                    </div>
            <?php endwhile; wp_reset_query(); ?>    
</div>

Both the posts and custom post type share 2 WP categories (simple, advanced). If the user opens up an article which is posted in "advanced" category , i would like the custom post type only to show posts from "advanced" category...

Hope you understood what i mean, english isnt my first language... Thanks for the help!

Upvotes: 0

Views: 158

Answers (1)

acobster
acobster

Reputation: 1657

If I understand what you mean, your simple/advanced categories are mutually exclusive, so you can assume that there is one and only one category on the current blog post. Assuming that's the case, you first want to get the current post's category with get_the_terms() and then pass that category to your custom query via tax_query.

Something like this should work:

// main loop up here...

// get terms (categories) from the current post
$cats = get_the_terms($post->ID, 'category');

// default args for querying our manual posts
$manuals_query_args = array(
  'post_type' => 'manuals',
  'posts_per_page' => 3
);

// filter manuals by category, if and only if current post is categorized
if (isset($cats[0])) {
  $cat_id = $cats[0]->term_id;
  $manuals_query_args['tax_query'] = array(
    // note that tax_query needs to be an *array of arrays* -
    // in this case, an array containing just one array
    array(
      'taxomony' => 'category',
      'field'    => 'id',
      'terms'    => $cat_id
    )
  );
}

$loop = new WP_Query($manuals_query_args);

You may have to tweak it, I didn't test this code.

Upvotes: 0

Related Questions