0

I have a custom post type called 'Podcasts' but none of the posts are showing in the archive widget. Only the main query (blog posts) are being shown.

I've tried adding this code below to include the Podcasts posts, but this only allowed them to show up in the archive template, which is great but not quite there yet.

/* Show Podcasts posts in archive */
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array('nav_menu_item','post','podcasts');
    $query->set('post_type',$post_type);
    return $query;
    }
}

How can I get the Podcasts posts to show up in the archive widget?

Edit: For clarity, I'm talking about when I click the dropdown in the native archives widget. The days/months that should have podcast posts don't even show up. Only the day/months for the blog posts show.

Edit 2: I tried dropping this into functions.php and now the months containing the podcasts posts show up, but when I click on those months, it just leads me to the 404 WordPress template. However, when I click on a month containing a post from the main query, it takes me to the archive template listing those posts.

add_filter( 'getarchives_where', 'custom_getarchives_where' );
function custom_getarchives_where( $where ){
    $where = str_replace( "post_type = 'post'", "post_type IN ( 'post', 'podcasts' )", $where );
    return $clauses;
}
  • @Pieter I saw that thread before posting mine and tried the solutions but unfortunately they didn't work in helping the posts show up in the archive widget. Anyway, I've been trying a few things after posting the original question and I added a bit which enables the months (containing the podcasts posts) to actually show up, but when I click on them, they lead me to the 404 WordPress template. – Desi Jul 16 '14 at 8:17
  • You will get a 404 error with the code in edit 2. You have to change the post type 'videos' to your own post type which is 'podcasts' ;-) – Pieter Goosen Jul 16 '14 at 8:24
  • @Pieter I guess you didn't catch my ninja edit :) I changed it to 'podcasts' but it still produces the same result. – Desi Jul 16 '14 at 8:26
  • Lol, ya, missed that. Posting from my phone, so I have to refresh to get updates. I did play around with the code before leaving for work to make sure it worked before filing the duplicate. Everything is at home, but I can remember having the same issue. I've cleared that by using pre_get_posts to include the post type in archive pages. – Pieter Goosen Jul 16 '14 at 8:35
  • You might try looking at the code in the wordpress.org/plugins/ultimate-posts-widget plugin to see how they do it. – Brad Dalton Jul 16 '14 at 9:19
5

For the sake of explanation, here is how to solve the problem.

By default, wordpress doesn't include custom post types in the main query, that is why you only get posts from the post type post in the archive pages. Also, widgets by default also does not include custom post types.

As @birgire described in the post I flagged as duplicate

The Archive widget is using wp_get_archives() to display the archive.

If you want to target all the wp_get_archives() functions, you can use the getarchives_where filter to add your custom post type:

add_filter( 'getarchives_where', function ( $where )
{
    $where = str_replace( "post_type = 'post'", "post_type IN ( 'post', 'podcasts' )", $where );
    return $where;
});

That should take care of displaying custom post type posts in the archive widget, but these will still not appear and give you a 404 error if you go to the specific archive, simply because you still have to include your custom post type in your archive page (archive.php)

To accomplish this, you can use the pre_get_posts action to add these custom post types to the main query before it is run. To specifically target archive pages, you can use the is_archive() conditional tag in conjuction with your function

add_action( 'pre_get_posts', function ( $query ) 
{
  if (    !is_admin() 
       && $query->is_main_query() 
       && $query->is_archive()
   )
     $query->set( 'post_type', array( 'post', 'podcasts' ) );
});
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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