Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to get a list of the most recent post from every category/taxonomy. I have found a solution for this, but I want the overall list to be sorted by post date. At the moment it is sorted by category/taxonomy name.

So if I post in cat A, then in Cat B and last in cat C: the post would be ordered: C, B, A (latest first). If I then post in cat B the order should change to: B, C, A

Really would like it to work with custom taxonomy but default categories could work for me too.

The code I have so far is from this site: http://wptheming.com/2012/08/display-the-most-recent-post-in-each-category/

//Retrieves all the terms from the taxonomy portfolio_category
//http://codex.wordpress.org/Function_Reference/get_categories
$cat_args = array(
    'type' => 'post',
    'orderby' => 'name',
    'order' => 'ASC',
    'taxonomy' => 'tax_name');
$categories = get_categories( $cat_args );

    // Pulls the first post from each of the individual portfolio categories
    foreach( $categories as $category ) {
        $args = array(
            'posts_per_page' => 1,
            'post_type' => 'post',
            'tax_name' => $category->slug,
            'no_found_rows' => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false
        );
        $the_query = new WP_Query( $args );

        // The Loop
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <div id="item">
                <b><?php the_title(); ?></b>
                <?php the_content(); ?>
            </div>
        <?php endwhile;
        }
    // Reset Post Data
    wp_reset_postdata();
    ?>

EDIT: I tried editing the first part of the code to:

$cat_args = array(
    'type' => 'post',
    'orderby' => 'date',
    'order' => 'DESC',
    'taxonomy' => 'tax_name');
$categories = get_categories( $cat_args );

But it still shows the posts in order of taxonomy name?

share|improve this question
add comment

1 Answer

In your code - Find

$cat_args = array(
    'type' => 'post',
    'orderby' => 'name',
    'order' => 'ASC',
    'taxonomy' => 'tax_name');
$categories = get_categories( $cat_args );

Repalce

$cat_args = array(
    'type' => 'post',
    'orderby' => 'date',
    'order' => 'DESC',
    'taxonomy' => 'tax_name');
$categories = get_categories( $cat_args );

share|improve this answer
    
I tried that first, but somehow it still shows the post in order of taxonomy name... :/ –  kaas Oct 15 '13 at 10:49
    
remove 'taxonomy' => 'tax_name' from array $cat_args.... –  sanchu Oct 15 '13 at 10:53
    
That gives me no results/post ? It also forces the code to look only at default categories. Would like it to work with my custom taxonomy ('tax_name' in this example) –  kaas Oct 15 '13 at 11:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.