0

Is it possible to query posts from latest category only? I know that categories dont store date but I know also that newer categories have bigger IDs

My standard query:

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

        <?php if (!empty($paged)) {
            $paged = $paged;
        } elseif (get_query_var('paged')) {
            $paged = get_query_var('paged');
        } elseif (get_query_var('page')) {
            $paged = get_query_var('page');
        } else {
            $paged = 1;
        }
        $argsmi = array('post_type' => 'post', 'posts_per_page' => 6, 'order' => 'DESC', 'paged' => $paged);
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query($argsmi);
        if ($wp_query->have_posts()) : ?>
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
                <!-- do stuff -->
            <?php endwhile; ?>
            <?php if (function_exists('wp_paginate')) {
                wp_paginate();
            } ?>
            <?php $wp_query = null;
            $wp_query = $temp;
            wp_reset_query(); ?>
        <?php endif; ?> 


    <?php endwhile; ?>
<?php endif; ?> 
  • To make it more solid, you could actually do it with 'term post date', rather than by the ID, using term meta. Using term meta, give the taxonomy terms meta for 'creation date' and set the default value to now. Then you could write your WP_Query accordingly. [link]smashingmagazine.com/2015/12/… – Dan. Nov 26 '16 at 15:36
  • ... Doing it by ID isn't solid as may delete terms, leaving the one with highest ID in the database not be the newest one. – Dan. Nov 26 '16 at 15:38
  • @Dan. in what case would deleting terms result in the remaining term with highest ID not then being the newest? IDs don't get reused, they only increment. – Milo Nov 26 '16 at 19:58
  • is the real question about how to find the category with the highest ID? – Michael Nov 27 '16 at 2:16
  • @Michael you're right sorry wasn't thinking straight – Dan. Nov 27 '16 at 10:08

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.