0

I have a template created where I want to call up all post types, but display only those from a common category. The results I'm getting are all normal blog posts and some from one of the custom post types. I have rechecked multiple times and I am using the correct template on my test page. I have cleared the cache.

On the test page at this link: https://moneysmartfamily.com/budgeting-category-test/ you'll see a list of blog posts. The category I'm trying to display is "budget". The category for each post is at the end of the meta line. You'll see that more than "budget" is being pulled up. If you scroll down to July 25, you'll see posts without categories mentioned. Those are from the CPTs. (I don't understand why they don't have categories listed, though. But they are there.)

My query args should list only 'budget' category posts from all post types, but I'm getting all the categories showing from regular posts. How can I ensure that I only get the category I want to see when this runs?

I created these custom post types in a Meta Box plugin, and each CPT has a custom taxonomy. I'm wondering if I need to combine the taxonomy names in a way to ensure they are being used. Can anyone point out examples of how this has been done elsewhere so I can figure this out?

Here is my template code so you can see what I'm trying to do.

<?php
/**
 * Template Name: Category - Budgeting
 *
 *
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

get_header(); 

    $args = array(
        'category_name'  => array(
            'budget'
        ), // category_name (string) – use category slug.
    );


    // This query should now only have the post_type in the args above that are in the category budget.
    $this_query = new WP_Query( $args ); ?> 


<div id="primary" class="site-content">
    <div id="content" role="main">

    <?php 
    if ( $this_query->have_posts() ) :



        while ( $this_query->have_posts() ) : $this_query->the_post(); 
            $post_id = get_the_ID(); // In case you need to use the post ID
            ?>

            <?php if( has_post_thumbnail() ): ?>
        <header class="entry-header">

            <h1 class="entry-title">
                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h1>
            <span class="post-meta">Author  <?php the_author_posts_link(); ?> | <?php the_time('F jS, Y'); ?>  | <?php the_category(', '); ?></span>

        </header><!-- .entry-header -->


              <div class="entry-summary">
            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?></a><?php the_excerpt(); ?>
        </div><!-- .entry-summary -->


            <?php endif; ?>

        <?php
        endwhile;
        wp_reset_postdata();
    endif;
    ?>
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

UPDATE: After some detective work figuring out how to do multiple tax queries and several tries, I tried this setup in the args and got blank content. Is there something that stands out to you as to why this isn't working? I'm learning queries right now so some advice would be very appreciated. Thanks!

     $args = array(
        'post_type' => array(
            'post',
            'media-appearance',
            'members-archive',
            'money-saving-tips',
            'review'
        ),
    'tax_query'  => array(
    'relation' => 'AND',
    array(
        'taxonomy'         => 'post',
        'terms'            => 'budget',
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
    array(
        'taxonomy'         => 'money-savings-tips-categories',
        'terms'            => 'budget',
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
    array(
        'taxonomy'         => 'reviews-categories',
        'terms'            => 'budget',
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
    array(
        'taxonomy'         => 'media-appearance-categories',
        'terms'            => 'budget',
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
    array(
        'taxonomy'         => 'member-archives-categories',
        'terms'            => 'budget',
        'field'            => 'slug',
        'operator'         => 'IN',
    ),
),
);
1
  1. category_name should take a string, not an array I believe
  2. post_type is not mentioned as an arg, and thus defaults to 'post'

so try:

    $args = array(
      'post_type' => 'any', //or use cpt slug as string, or array of strings if multiple
      'category_name' => 'budget', //use 'budget,premium' if you want posts with budget OR premium, use 'budget+premium' if you want budget AND premium. 
    );

    $this_query = new WP_Query( $args ); ?> 
  • Yes! That worked. I can see only budget categories now. So simple. Thank you, Jess! – David Borrink Oct 3 at 15:28
0

In your multi-taxonomy query just change the relationship to "OR" . that should do the work.

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.