Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am not sure how to fetch specific custom post type where taxonomy is for example february. Generally I'm trying to have custom data based on months.

Registering custom post type

add_action( 'init', 'plants' );
function plants() {
    $args = array( labels and etc   );
    register_post_type( 'plants' , $args );
}

Registering taxonomy for custom post type

add_action('init', 'months');
function months() {
    $args =  array ( labels and etc );  
register_taxonomy('months', 'plants', $args);
}

Fetching data

$args = array( 'post_type' => 'plants'  ); 
$plants= new WP_Query($args);
if ($plants->have_posts()) : while ($plants->have_posts()): $plants->the_post(); ?>
           <?php the_title(); ?>
    <?php endwhile; ?>
<?php endif; ?>

So everything so far works, I get 3 items. But how do I fetch only items with specific taxonomy? Following doesnt do anything

$args = array(
    'post_type' => 'plants',
    'months'=>'february'
    );
share|improve this question

1 Answer 1

Your code should work if you are using the correct information. You can directly work with the query variables as you did. Just remember to reset your post data after your custom query, very very important!

You can also try using the tax_query in WP_Query

$args = array(
    'post_type' => 'plants',
    'tax_query' => array(
        array(
             'taxonomy' => 'months',
             'field' => 'slug',
             'terms' => 'february'
          )
       )
);

I want to however point out that you should regsiter your custom post type and taxonomy inside the same function as you are currently creating multiple instances that is hooked to the same hook init

It is always important to keep your code organised. If you have 20 separate things you need to add to the init hook, create one function, and hook that to init. Don't create 20 separate functions. One thing to remember though, the order in which you add your functionalities inside your function is very important if one functionality depends on another. Adding them in reverse order will break your functionality

Also, when registering a new taxanomy, it is good pactice to use register_taxonomy_for_object_type() to add that taxonomy your custom post type

Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during parse_request or pre_get_posts.

share|improve this answer
    
Wasn't this useful? If you solved your problem with your own solution, feel free to post you solution as an answer and accept it. If my solution worked, consider accepting my answer. Please help us keep this site constructive, not just for us, but for your sake and the sake of others. :-) –  Pieter Goosen Aug 4 at 4:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.