views:

50

answers:

2

Hello,

i try to setup a yearly (grouped by month) archive for custom post types in WordPress. But my code did not work as aspected. Maybe it is obiviously for someone who is more familar with WordPress and PHP but i can't get it work.

The code below is grouping by month but each post type by itself. Maybe i need to merge booth. But how?

<?php query_posts (array ('post_type' => array('images', 'articles')));?>

    <?php
        if (have_posts()) : while (have_posts()) : the_post();

        // save month to a variable
        $month = the_date('M', '', '', FALSE);

        // If not used before print it 
        if ($month != $m_check) {
            echo "<h2>" . $month . "</h2>";
        }

        // save month to check variable
        $m_check = $month;
    ?>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>

    <?php endwhile; ?>
    <?php endif; ?>
<?php wp_reset_query();?>

Regards, Steve

+1  A: 

You will need this: http://wordpress.org/extend/plugins/posts-to-posts/ to achieve what you want with custom posts.

negatif
A: 

Ok... after a cup of tea i got it with an other (probably better) approach.

Solution

<?php

    $args = array(
        'post_type' => array('images', 'articles'),
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => null,
        );

    $posts = get_posts($args);

    if ($posts) {
        foreach ($posts as $post) {
            setup_postdata($post);
            $month =  mysql2date('m', $post->post_date);

            if ($month != $check) {
                echo "<h2>" . $month . "</h2>";
            }

            // save month to check variable
            $check = $month;

            echo $post->post_title;
            echo '<br/>';
        }
    }

    ?>

Output

07
    Eagle creek
    Lorem Ispum dolor
    Vancouver Island
    Ottawa
    Vancouver
06
    Losabim oxygenium

Now it needs just a bit beautifying and i am done. By the way @negatif, thank you for your suggestion.

gearsdigital