2

I need help with WP custom taxonomy and custom post types. I never find a working solution so far. Please accept my apologies if this has a simple solution. I tried my best to find solution everywhere first.

Explanation of issue -

  1. I have one taxonomy called "department".
  2. I have 3 CPT called "course", "faculty", "library".

All 3 CPT will use same taxonomy "department". I have all setup successfully and assigned the "department" taxonomy to all 3 CPT.

Now I am trying to show posts (all 3 CPT posts) by "department" taxonomy terms (for instance, I have terms "x","y","z" for the taxonomy "department"). I have a template file called "taxonomy-department.php", which has simple loop -

<?php if (have_posts()) : ?>

        <ul> 

        <?php while (have_posts()) : the_post(); ?> 
            <li>
                <?php the_title(); ?>
            </li>
        <?php endwhile; ?>

        </ul>
        <?php else : ?>

        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

The above loop inside the taxonomy template, is not working and showing no posts. Just going to else statement. I am not sure how to achieve this.

If I fetch posts successfully (with your help), is it also possible to filter posts by CPT? (in the taxonomy template file)

Any help is much appreciated.

3 Answers 3

3

WP defaults to showing normal native Posts in archives. It won't automagically pick up which post types you want in your archive.

You will have to adjust main query for it to explain that to it, with something like:

add_action( 'pre_get_posts', function ( WP_Query $query ) {

    if ( $query->is_main_query() && $query->is_tax( 'department' ) ) {
        $query->set( 'post_type', [ 'course', 'faculty', 'library' ] );
    }
} );
4
  • Great! Thanks @Rarst, This code works as the WP by default do not know custom taxonomy to query. I am using a bit modified code as I don't need this code to run in WP admin. Here is the code - add_action( 'pre_get_posts', 'my_tax_query' ); function my_tax_query($query){ if ( !is_admin() && $query->is_main_query() && $query->is_tax( 'department' ) ) { $query->set( 'post_type', array( 'course', 'faculty', 'library' ) ); } }; WP tells us to use taxonomy-{taxonomy}.php file to use for custom taxonomy, so WP should really query automatically!
    – Amit
    Commented Aug 21, 2016 at 13:45
  • There must be a reason that WP do not query for custom taxonomy automatically. If anyone knows it why, please let me know. Also I wonder, now is there a way to filter those posts with CPT?
    – Amit
    Commented Aug 21, 2016 at 13:51
  • I don't follow what you mean by "filter posts with CPT". Might or might not be better asked as a separate question.
    – Rarst
    Commented Aug 21, 2016 at 13:52
  • OMG @Rarst! I have been tearing my hair out for 3 days, trying every function I could find in order to get my taxonomy pagination to work properly. YOURS FINALLY DID IT!!! THANK YOU THANK YOU THANK YOU!!!
    – Laura Sage
    Commented Feb 13, 2020 at 16:23
1

I like to Thank @Rarst for his contribution to the answer. Here is the final piece of code -

add_action( 'pre_get_posts', 'my_tax_query' );

function my_tax_query($query){

    if ( !is_admin() && $query->is_main_query() && $query->is_tax( 'department' ) ) {
        $query->set( 'post_type', array( 'course', 'faculty', 'library' ) );
    }
};

Place the code inside of the functions.php file. This will grab all posts (for the term page) from for all post types that you have.

For example - if your term page is http://domain-name.com/taxonomy/term1/ You will get all posts for "term1" for all post types.

Now, can we filter posts by "post types" ? Is there a way to do that? Thanks!

1
  • Hi @Amit , I found this question and answer after I didn't get any answers for my own, very similar question here: wordpress.stackexchange.com/q/353983/102362 Yours is more than 3 years old - have you found any other solution in the meanwhile? I am still wondering if such a query (for a custom taxonomy) is not possible with the default loop/query as you had it in your question. It seems strange that the default query wouldn't output custom post types...
    – Johannes
    Commented Dec 6, 2019 at 16:06
0

Please try this-

$post_type = array('course', 'faculty', 'library');
$terms = array( 'x-tax-slug', 'y-tax-slug', 'z-tax-slug' );
$args = array(
    'post_type' => $post_type,
    'tax_query' => array(
        array(
            'taxonomy' => 'department',
            'field'    => 'slug',
            'terms'    => $terms,
        ),
    ),
);
$query = new WP_Query( $args );

<?php if (have_posts()) : ?>

        <ul> 

        <?php while ($query->have_posts()) : $query->the_post(); ?> 
            <li>
                <?php the_title(); ?>
            </li>
        <?php endwhile; ?>

        </ul>
        <?php else : ?>

        <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

<?php wp_reset_postdata(); // reset the query ?>

So here we are running a custom query based on your department taxonomy and there custom post type. And at last we are resetting the query.

5
  • The intent of the question seems to be to use native term archive for it, overriding main query for archive in template leads to worse performance and other issues (especially pagination).
    – Rarst
    Commented Aug 20, 2016 at 17:49
  • Thanks for your answer. I understand the query you have made. I have used this query before (without the terms defined). But this returns all posts in all terms. More over, this is not dynamic. How do I know what terms will be used by user? I should not predefine the terms in tax query. Though CPT can be pre defined in query. But this query will not work.
    – Amit
    Commented Aug 21, 2016 at 2:53
  • Offcourse it could be dynamic. Just made the post_type and terms different variable and made the array of the variable dynamic. It would work. I've edited the answer. See that. Here now you need to change the value of the $post_type and $terms dynamically so you can get dynamic query result. And for know the query terms after the query, you can store them in session and can get them in another page.
    – CodeMascot
    Commented Aug 21, 2016 at 4:50
  • Well as I said earlier, this code will give me all posts in each terms page! This is not what I am looking for. For example, if you go to - http://domain-name.com/department/term1/ OR http://domain-name.com/department/term2/ - No matter which "term" page you are in, you will get all posts for all terms. If you are in http://domain-name.com/department/term1/, you will get results for "term1", "term2" and so on. All results. But we should get posts for only "term" page that we are in. I hope this make sense?
    – Amit
    Commented Aug 21, 2016 at 13:26
  • But I think - If I grab the term slug from the URL (by $_GET) may be that will work for individual term. Is that correct? Then this code is useful.
    – Amit
    Commented Aug 21, 2016 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.