Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9275471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:35:43+00:00 2026-06-18T16:35:43+00:00

I have a custom post type news in my WordPress site. I am using

  • 0

I have a custom post type “news” in my WordPress site. I am using Advanced Custom Fields plugin to add meta data to each post.

I want to create an array of news items as an archive:

[2013]
    [January] => 5
[2012]
    [January] => 20
    [February] => 10
[2011]
    [April] => 30

I managed to get this working using:

    global $wpdb;
    $news = $wpdb->get_results(
        "SELECT wp_posts.post_date, COUNT(wp_posts.ID) as count
         FROM $wpdb->posts
         WHERE
         wp_posts.post_type = 'news' AND
         wp_posts.post_status = 'publish' AND
         wp_posts.post_date <= CURDATE() AND
         wp_posts.post_date >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)
         GROUP BY YEAR(wp_posts.post_date), MONTH(wp_posts.post_date)
         ORDER BY wp_posts.post_date DESC", 
         ARRAY_A);

    $archive = array();
    foreach ($news as $post):
        $year = date('Y', strtotime($post['post_date']));      
        $month = date('m', strtotime($post['post_date']));     
        $month_name = date('F', strtotime($post['post_date']));
        $post['url'] = 'NOT SURE ABOUT URL';
        $archive[$year][$month_name] = $post;
    endforeach;

I need to be able to link to specific years and months using http://example.com/2012/ and http://example.com/2012/10/.

As this involves a custom post type “news” I’m unsure how to make this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:35:44+00:00Added an answer on June 18, 2026 at 4:35 pm

    In order to do what you require, you need to modify WordPress rewrites to be able to capture the year/month/etc post data for a custom post type.

    You may do this with the following code:

    /**
     * Custom post type date archives
     */
    
    /**
     * Custom post type specific rewrite rules
     * @return wp_rewrite             Rewrite rules handled by WordPress
     */
    function cpt_rewrite_rules($wp_rewrite) {
        $rules = cpt_generate_date_archives('news', $wp_rewrite);
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
        return $wp_rewrite;
    }
    add_action('generate_rewrite_rules', 'cpt_rewrite_rules');
    
    /**
     * Generate date archive rewrite rules for a given custom post type
     * @param  string $cpt        slug of the custom post type
     * @return rules              returns a set of rewrite rules for WordPress to handle
     */
    function cpt_generate_date_archives($cpt, $wp_rewrite) {
        $rules = array();
    
        $post_type = get_post_type_object($cpt);
        $slug_archive = $post_type->has_archive;
        if ($slug_archive === false) return $rules;
        if ($slug_archive === true) {
            $slug_archive = $post_type->name;
        }
    
        $dates = array(
            array(
                'rule' => "([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})",
                'vars' => array('year', 'monthnum', 'day')),
            array(
                'rule' => "([0-9]{4})/([0-9]{1,2})",
                'vars' => array('year', 'monthnum')),
            array(
                'rule' => "([0-9]{4})",
                'vars' => array('year'))
            );
    
        foreach ($dates as $data) {
            $query = 'index.php?post_type='.$cpt;
            $rule = $slug_archive.'/'.$data['rule'];
    
            $i = 1;
            foreach ($data['vars'] as $var) {
                $query.= '&'.$var.'='.$wp_rewrite->preg_index($i);
                $i++;
            }
    
            $rules[$rule."/?$"] = $query;
            $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
            $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
            $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
        }
        return $rules;
    }
    

    You will notice that I have hard-coded news in to the $rules = cpt_generate_date_archives('news', $wp_rewrite); portion of the code. You can change this as needed.

    With this code, you should be able to navigate to http://yoursite.com/news/2013/02/ and receive an archive listing for that specific post type for that specific time.

    For completeness, I will include how to generate a monthly archive widget in order to utilize this.

    /**
     * Get a montlhy archive list for a custom post type
     * @param  string  $cpt  Slug of the custom post type
     * @param  boolean $echo Whether to echo the output
     * @return array         Return the output as an array to be parsed on the template level
     */
    function get_cpt_archives( $cpt, $echo = false )
    {
        global $wpdb; 
        $sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' GROUP BY YEAR($wpdb->posts.post_date), MONTH($wpdb->posts.post_date) ORDER BY $wpdb->posts.post_date DESC", $cpt);
        $results = $wpdb->get_results($sql);
    
        if ( $results )
        {
            $archive = array();
            foreach ($results as $r)
            {
                $year = date('Y', strtotime( $r->post_date ) );
                $month = date('F', strtotime( $r->post_date ) );
                $month_num = date('m', strtotime( $r->post_date ) );
                $link = get_bloginfo('siteurl') . '/' . $cpt . '/' . $year . '/' . $month_num;
                $this_archive = array( 'month' => $month, 'year' => $year, 'link' => $link );
                array_push( $archive, $this_archive );
            }
    
            if( !$echo )
                return $archive;
            foreach( $archive as $a )
            {
                echo '<li><a href="' . $a['link'] . '">' . $a['month'] . ' ' . $a['year'] . '</a></li>';
            }
        }
        return false;
    }
    

    To use this function just supply the slug of the custom post type, ie: get_cpt_archives( 'news' ). This will return an array of unique Year/Dates/Links, ie:

    Array
    (
        [0] => Array
            (
                [month] => February
                [year] => 2013
                [link] => http://yoursite.com/news/2013/02
            )
    
        [1] => Array
            (
                [month] => January
                [year] => 2013
                [link] => http://yoursite.com/news/2013/01
            )
    
    )
    

    You can loop through these with a foreach and output them however you want.

    Alternatively, you can use get_cpt_archives( 'news', true ) which will automatically echo each item wrapped in an <li> linking to its specific archive.

    The formatting of the output is not exactly what you wanted, so you will have to tweak it a bit to get it to display in the

    Year
        Month
        Month
    Year
        Month
    

    format that you require.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using magic fields and have defined a custom post type called collection
I have a Custom Post Type, 'ioni_codex' I am using built-in Wordpress category as
I have created a wordpress custom post type. I am using custom field template
I have created Custom slideshow post type.Images set as featured images for each new
I am using a custom post type in my wordpress theme, and I need
I am currently working on a Wordpress site I have created some custom Post
I have setup a custom Post Type in WordPress and have a question. How
I have setup a custom post type in Wordpress. It is a section where
I have a series of posts in a custom post type. They each have
I have two taxonomies in wordpress under my Events custom post type. One taxonomy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.