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 duringparse_request
orpre_get_posts
.