0

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?

6
  • Regarding the way you have to call in all the post types in an array is correct and standard behaviour. Also to note an archive page is specifically for one post type only.Try using a tax_query in your arguments array as opposed to the category. Also can you confirm what you is being returned by get_theme_option('featured_posts_category'); is it an id, object etc
    – user10106
    Commented Jan 15, 2014 at 21:49
  • @Mike, Thanks. My get_theme_option() is returning a category ID and it's proven code that was working before I created the Custom Post Types.
    – Sparky
    Commented Jan 15, 2014 at 22:01
  • your get_posts needs a posts_per_page argument if you want more than whatever the default number per page is.
    – Milo
    Commented Jan 15, 2014 at 22:04
  • @Milo, what if I want "all"?
    – Sparky
    Commented Jan 15, 2014 at 22:05
  • set it to -1 to get all.
    – Milo
    Commented Jan 15, 2014 at 22:07

2 Answers 2

1

Your get_posts call needs a posts_per_page argument if you want more than whatever the default number per page is. Use -1 to retrieve all matching posts.

$featured_posts = get_posts( 
    array(
        'post_type' => array( 'post', 'books', 'cds', 'maps' ),
        'category' => get_theme_option('featured_posts_category'),
        'posts_per_page' => -1
    )
);
0

Try removing the parameter:

'category' => get_theme_option('featured_posts_category')

and replace with:

'tax_query' => array(
    array(
        'taxonomy' => 'category',
        'field' => 'id',
        'terms' => get_theme_option('featured_posts_category')
    )
)

I believe you are facing issues because the parameters named "category" or "cat" used inside of queries are inherently related to the default "post" post_type.

2
  • The result is the same... only getting results from one category.
    – Sparky
    Commented Jan 15, 2014 at 22:12
  • @Milo nailed it. Your code is working as broken as mine. Had to add the 'numberposts' => -1 to get all the posts.
    – Sparky
    Commented Jan 15, 2014 at 22:15

Your Answer

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

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