TransWikia.com

wp_get_archives() not working for my custom post type

WordPress Development Asked by Best Dev Tutorials on October 30, 2021

I have my custom post type working, but I can’t seem to get the archives working.

Here is my code for getting the archives:

  <ul>
   <?php 

   $args = array(
    'type'            => 'monthly',
    'limit'           => '',
    'format'          => 'html', 
    'before'          => '',
    'after'           => '',
    'show_post_count' => false,
    'echo'            => 1,
    'order'           => 'DESC',
    'post_type'     => 'my_custom_post_type'
  );

  wp_get_archives( $args ); ?>
  </ul>

Here are the args for my CPT:

$args = array(
    'label'                 => __( 'My Custom Post Type', 'realtextdomain' ),
    'description'           => __( 'What We Are Reading Custom Post', 'realtextdomain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
    'hierarchical'          => false,
    'public'                => false,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => true,
    'publicly_queryable'    => false,
    'menu_icon'             => 'dashicons-book',
    'capability_type'       => 'post',
    'rewrite' => array('slug' => 'my_custom_post_type_slug'),
);

register_post_type( 'my_custom_post_type', $args );

I have flushed permalinks, but still nothing showing up for me… any ideas on what I’m doing wrong?

One Answer

wp_get_archive() does not work natively with custom post type, even if the official documentation let suppose it's possible to use CPT as argument ( https://codex.wordpress.org/Template_Tags/wp_get_archives)

post_type ("string") Limit archives to a post type. Default is 'post'. (since Version 4.4)

You can find discussion and possible workaround in this stackoverflow post: https://stackoverflow.com/questions/8943251/wp-get-archives-for-custom-post-type-not-working

and here: Custom Post Type Archives by Year & Month?

Please note also that in your CPT register_post_type() args you should always set public to "true" to have a public accessible url

Answered by Andrea Somovigo on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP