• I am trying to show one most recent post per category at HOME.

    Starting from the Sandbox theme,
    I modified to the LOOP as follows,
    but it doesn’t work.

    <?php
    $args = array(
    ‘type’ => ‘post’,
    ‘orderby’ => ‘count’,
    ‘order’ => ‘ASC’,
    ‘hide_empty’ => 1);
    $categories = get_categories($args);
    foreach ($categories as $category) {
    $options = array(
    ‘cat’ => $category->Cat_ID,
    ‘showposts’ => 1,
    ‘orderby’ => ‘rand’);
    $my_query = new WP_Query($options);
    while ($my_query->have_posts()) : $my_query->the_post();
    ?>
    //the content
    <?php wp_reset_postdata(); ?>

    **
    I run this locally and
    sometime, it shows one post per category as supposed to, but other times,
    it shows two posts per category or shows a same post twice, i.e., it behaves erratically.

    Please help.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query to pull one post per category’ is closed to new replies.