1

there is one custom post type News there are 3 taxonomy terms(like category).

1). press-release

2). in-the-news

3). events

i have list out all this three category. now what i am looking for is to display the archive for each category. for example:

      Events
       2014
       2013
       2012

for ex: if i click on 2014 under events that will return the events posts posted in 2014

i want to get events posts by year. for that you need to make you i have used following function to list out the posts

   function register_post_type_rewrite_rules($wp_rewrite) {
     $args = array('public' => true, '_builtin' => false); 
     $output = 'names';
     $operator = 'and';
     $post_types = get_post_types($args,$output,$operator);
     $url_base = ($url_base == '') ? $url_base : $url_base . '/';
     $custom_rules = array();
     $post_types = implode('|', $post_types);
     $custom_rules = array( "$url_base($post_types)/([0-9]+)/([0-9]{1,2})/([0-9]{1,2})/?$" =>
       'index.php?post_type_index=1&post_type=' . $wp_rewrite->preg_index(1) . '&news_category='. $wp_rewrite->preg_index(2) .'&year=' . $wp_rewrite->preg_index(3) . '&monthnum=' . $wp_rewrite->preg_index(4) . '&day=' . $wp_rewrite->preg_index(5),       

      "$url_base($post_types)/([0-9]+)/([0-9]{1,2})/?$"  =>
            'index.php?post_type_index=1&post_type=' . $wp_rewrite->preg_index(1) . '&news_category='. $wp_rewrite->preg_index(2) .'&year=' . $wp_rewrite->preg_index(3) . '&monthnum=' . $wp_rewrite->preg_index(4),         //year month

      "$url_base($post_types)/([0-9]+)/?$" =>
            'index.php?post_type_index=1&post_type=' . $wp_rewrite->preg_index(1) . '&news_category='. $wp_rewrite->preg_index(2) .'&year=' . $wp_rewrite->preg_index(3) //year
                        );
      $wp_rewrite->rules = array_merge($custom_rules, $wp_rewrite->rules); // merge existing rules with custom ones

      return $wp_rewrite;
     }

To display the Archive i have create such url.

     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)  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 ) );
        $taxonmoy = get_term( 'events'  , 'news_category', $output, $filter ) ;
        $link = get_bloginfo('siteurl') . '/' . $cpt . '/'.get_query_var('term').'/'. $year ;
        $this_archive = array(  'year' => $year, 'link' => $link );
        array_push( $archive, $this_archive );
    }

    if( !$echo )
        return $archive;
        echo '<ul class="nav nav-list doc_first_level">';
    foreach( $archive as $a )
    {
        echo '<li><a href="' . $a['link'] . '">' . $a['year'] . '</a></li>';
    }
    echo '</ul>';
      }
        return false;
    }

what i am looking for is to get custom taxonomy posts by year.

for ex: http://domain.com/news/events/2014

this will return the 2014 posts of events term.

right now it shows 400 not found

any help would be greatly appreciated.

Your Answer

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

Browse other questions tagged or ask your own question.