i attempt to create an annual (arranged by month) archive for custom publish types in WordPress. But my code didn't act as aspected. Maybe it's obiviously for somebody who's more familar with WordPress and PHP however i can't have it work.

The code below is grouping by month but each publish type alone. Maybe i have to merge booth. But exactly 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

You'll need this: http://wordpress.org/extend/plugins/posts-to-posts/ to attain what you would like with custom posts.

Ok... after a mug of tea i first got it with some other (most likely 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 requires just a little decorating and i'm done. Incidentally @negatif, appreciate your suggestion.