I am using a custom post type to add/display PDF documents (posts) on my website.
taxonomy - wpl_documents_category
post type - post_documents
All is working well. However, I want to display the posts exclusively in their category - whether it be in a parent or child category.
Example Below
Hierarchy: Parent Cat -> Child Cat 1 -> Child Cat 2
Post Name: Dummy Doc
Post Category: Child Cat 2
By default, WordPress will show this post in all 3x levels. However, what I want is to display it in the category it belongs to (Child Cat 2) only.
Query Below
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
I can't figure this out and have tried various methods/solutions, but to no avail.
Any ideas/information on the above is greatly appreciated!
EDIT
Complete Query
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$pid = $post->ID;
$document_file = get_post_meta( $pid, 'wpl_document_file', true);
$document_file_size = get_post_meta( $pid, 'wpl_document_file_size', true);
$document_authors = get_post_meta( $pid, 'wpl_document_authors', true);
?>
HTML markup goes here with calls to echo the above information
Display Categories
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); if ($term->parent == 0) { ?>
<?php wp_list_categories('taxonomy=wpl_documents_category&depth=1&title_li=&child_of=' . $term->term_id); ?>
Here is my complete archive-post_documents.php
<?php
/**
* The default template for displaying documents archive
*
* @package WordPress
*/
?>
<?php get_header(); ?>
<div id="main" class="site-main container_12">
<div id="primary" class="content-area ms grid_12">
<div id="content" class="site-content">
<?php if (category_description( $category ) == '') { ?>
<?php } else { ?>
<article class="single">
<div id="subcats" class="entry-content">
<h2>Description</h2>
<?php echo category_description( $category_id ); ?>
</div>
</article>
<?php } ?>
<?php
$term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => 0,
'show_count' => 0
) );
if($children) {
?>
<article class="single">
<div id="subcats" class="entry-content">
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) { ?>
<h2>Browse Sub-categories:</h2>
<ul>
<?php wp_list_categories('taxonomy=wpl_documents_category&depth=1&title_li=&child_of=' . $term->term_id); ?>
</ul>
<?php } else { ?>
<h2>Browse Sub-categories:</h2>
<ul>
<?php wp_list_categories('taxonomy=wpl_documents_category&&title_li=&child_of=' . $term->term_id); } ?>
</ul>
</div>
</article>
<?php } ?>
<div> </div>
</div>
<div id="content" class="site-content js-masonry">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php //if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php
$pid = $post->ID;
$document_file = get_post_meta( $pid, 'wpl_document_file', true);
$document_file_size = get_post_meta( $pid, 'wpl_document_file_size', true);
$document_authors = get_post_meta( $pid, 'wpl_document_authors', true);
$icon = wplook_get_icon_name($document_file);
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('item'); ?>>
<?php if ( has_post_thumbnail() ) {?>
<figure>
<a title="<?php the_title();?>" href="<?php echo $document_file; ?>" download="<?php preg_replace("/[^a-zA-Z0-9_-]/", "", the_title);?>">
<?php the_post_thumbnail('document-image'); ?>
</a>
</figure>
<?php } else {?>
<figure>
<a title="<?php the_title();?>" href="<?php echo $document_file; ?>" download="<?php preg_replace("/[^a-zA-Z0-9_-]/", "", the_title);?>">
<img data-lazy-loaded="true" style="display: block;" src="http://www.absa.asn.au/absainc/wp-content/uploads/2014/09/pdficon-260x173.jpg" class="attachment-document-image wp-post-image" alt="pdficon" height="173" width="260">
</a>
</figure>
<?php } ?>
<div class="box-conten-margins">
<h1 class="entry-header">
<a title="<?php the_title(); ?>" href="<?php echo $document_file; ?>" download="<?php preg_replace("/[^a-zA-Z0-9_-]/", "", the_title);?>"><?php the_title(); ?></a>
</h1>
<div class="short-description">
<p><i class="<?php echo $icon; ?>"></i> <strong>Size:</strong> <span class="filesize"><?php echo $document_file_size; ?></span></p>
<?php if ($document_authors) { ?>
<p><strong>Author(s):</strong> <?php echo $document_authors; ?></p>
<?php } ?>
</div>
<div class="clear"></div>
<div class="entry-meta">
<a class="buttonsx" href="<?php echo $document_file; ?>" download="<?php preg_replace("/[^a-zA-Z0-9_-]/", "", the_title);?>"><i class="<?php echo $icon; ?>"></i> <?php _e('Download PDF', 'wplook'); ?></a>
<div class="clear"></div>
</div>
</div>
</article>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, there are no documents to list. This could either be because you are not an ABSA member or, there are simply no documents in this category.', 'wplook'); ?></p>
<?php endif; ?>
</div>
<div class="pagination-grid">
<?php wplook_content_navigation('postnav' ) ?>
</div>
</div>
<div class="clear"></div>
</div><!-- #main .site-main -->
<?php get_footer(); ?>
'tax_query' => [
- any ideas?