0

I want to make a custom post archieve template where I will display only the custom post by month..

I added this in archieve-news_letter.php

$args = array( 'post_type' => 'news_letter', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

/* Include the post format-specific template for the content. If you want to
                 * this in a child theme then include a file called called content-___.php
                 * (where ___ is the post format) and that will be used instead.
                 */
                get_template_part( 'newletter', get_post_format() );

where news_letter is my custom post...by this I am getting all post without any sorting of month, means I am getting all month post in December link.

to make archieve link I used this:

<li><?php wp_get_archives(array('type' => 'monthly','order'=>'ASC')); ?></li>

any help would be appreciate..

Thanks for your time and sharing knowledge... :)

1 Answer 1

0

I had some problem with wp_get_archives, you can try my solution of this issue

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(wp_posts.post_date), MONTH(wp_posts.post_date) ORDER BY wp_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;
}

put this in functions.php of your theme.

Then call this function where you need:

    <?php get_cpt_archives('post_type', true); ?>

'post_type' - change this to your post type name.

Second parameter:

true - it's a list with <li>

false - it's an array.

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.