I'm really struggling with this... I created a few Custom Post Types, let's say...
- Books
- CD's
- Maps
Now I also have a whole bunch of existing categories and some of these would apply to more than one Custom Post Type. For example, the "Available" category could be assigned to all of these Custom Post Types.
Now I'm running into a whole bunch of issues when I try to access the categories, either programmatically, or through the front end's archive pages.
For example, to put recent posts from all Custom Post Types on the home page, I had to do this...
function get_custom_posts( $query ) {
if ( ! is_admin() && empty($query->query_vars['suppress_filters']) ) { // is not in Dashboard (is_admin => Dashboard)
if ( is_home() ) { // home page
return $query->set( 'post_type', array( 'post', 'books', 'cds', 'maps' ) );
} else if .....
}
}
add_filter( 'pre_get_posts', 'get_custom_posts' );
And the more I discover working strangely, the more I have to go back and fix.
Presently I'm trying to retrieve all posts from the "Available" category for a slideshow and the Dashboard is showing me there are 19 posts in this category.
However, when I run this code...
<?php
global $post;
$featured_posts = get_posts(
array(
'post_type' => array( 'post', 'books', 'cds', 'maps' ),
'category' => get_theme_option('featured_posts_category')
)
);
foreach($featured_posts as $post) {
setup_postdata($post);
$slide_image = get_the_post_thumbnail($post->ID, 'medium');
echo '<a href="'.get_permalink($post->ID).'">'.$slide_image.'</a>';
....
}
?>
I'm getting far fewer posts than the 19 I expect and the ones I'm getting back are only coming from the "CDs" Custom Post Type and all other Post Types are ignored. Similarly, I get other unpredictable or strange results when selecting other categories. This all leads me to believe that I shouldn't be sharing the same category across multiple Post Types... can anyone confirm this or explain further what I may be missing here?
tax_query
in your arguments array as opposed to thecategory
. Also can you confirm what you is being returned byget_theme_option('featured_posts_category')
; is it an id, object etcget_theme_option()
is returning a category ID and it's proven code that was working before I created the Custom Post Types.get_posts
needs aposts_per_page
argument if you want more than whatever the default number per page is.-1
to get all.