In WordPress, I am trying to get posts from a custom post type 'color', custom taxonomy 'color-name', using the following:
Notes: I have a custom post type, Color, with custom posts that are titled things like, 'Coral', 'Peony'. I also have a custom taxonomy, color-name. Through a hook on saving a color post, categories in that custom taxonomy get created. Then, the custom post type Color, can be tagged with other related colors.
$slug = str_replace(" ", "_", $page_title);
$slug = strtolower($slug);
//Slug is - 'coral', 'peony', etc.
$args = array( 'post_type' => 'color',
'posts_per_page' => -1,
'tax_query' => array( array (
'taxonomy' => 'color-name',
'field' => 'slug',
'terms' => $slug
) )
);
$myposts = query_posts( $args );
I've tried many variations of this after Googling, and nothing is working - I either get all posts, or no posts. Here's another version of args I've tried: (results in no posts):
$args = array('color-name' => $page_title,
'post_type' => 'color',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
I've wrestled with this before and gave up and just made a custom sql call. Does anyone know definitively how to get this working through WordPress functions?
query_posts, it breaks the main query and in most cases outright fails in pagination. Also,caller_get_postsis longtime depreciated, was replaced withignore_sticky_posts- Pieter Goosen$slugis your problem. Are you sure the result from$slugis a string and not an array. Please edit your question and add your code for$slug. I want to have a look at that - Pieter Goosen