Problem displaying one post from each category

i had been banging my head for almost a day but could not resolve it.
i need to show one post from 4 xx categories here is my code

                <?php
                $cat_args = array(
                  'orderby' => 'name',
                  'order' => 'ASC',
                  'include' => '44,45,46,51'
                );

                $fcategories =   get_categories($cat_args); 

                foreach($fcategories as $fcategory) {
                    echo '<dl>';
                    echo '<dt> <a href="' . get_category_link( $fcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . '" ' . '>' . $fcategory->name.'</a></dt>';

                     $post_args = array(
                      'showposts' => 1,
                      'category' => $fcategory->cat_ID
                    );

                    $fposts = get_posts($post_args);

                foreach($fposts as $fpost) : setup_postdata($fpost);    ?>
                        <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
                    <?php

                    echo '<dd class="view-all"> <a href="' . get_category_link( $fcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . '" ' . '>View all posts in ' . $fcategory->name.'</a></dd>';
                    echo '</dl>';
                endforeach; 
                    }
                ?>

Every think seem to work perfectly except…. its showing only one post for all category while it should have shown one post from each category
to be more specific its showing like

Read More

cat-1 cat-2 cat-3 cat-4 (this is ok)
post-1 post-1 post-1 post-1 (this is the prob)

while it should have been

cat-1 cat-2 cat-3 cat-4
post-1 post-2 post-3 post-4

Please help

Related posts

Leave a Reply

1 comment

  1. I’ve checked this code localy, here is the working snippet:

            <?php
            $cat_args = array(
                    'orderby' => 'name',
                    'order' => 'ASC',
                    );
    
            $fcategories =   get_categories($cat_args); 
    
            foreach($fcategories as $fcategory) {
                echo '<dl>';
                echo '<dt> <a href="' . get_category_link( $fcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . '" ' . '>' . $fcategory->name.'</a></dt>';
    
                $post_args = array(
                        'posts_per_page' => 1,
                        'cat' => $fcategory->cat_ID
                        );
    
                $fposts = query_posts($post_args);
    
                while(have_posts()) : the_post();    ?>
                    <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
                    <?php
    
                    echo '<dd class="view-all"> <a href="' . get_category_link( $fcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . '" ' . '>View all posts in ' . $fcategory->name.'</a></dd>';
                echo '</dl>';
                endwhile; 
            }
            wp_query_reset();
    ?>
    

    Original answer:
    I think the problem might be here:

    $post_args = array(
      'showposts' => 1,
      'category' => $fcategory->cat_ID
    );
    

    you’re using category argument, it should be cat so try this code:

    $post_args = array(
      'showposts' => 1,
      'cat' => $fcategory->cat_ID
    );