Benjamin Oats
Benjamin Oats

Reputation: 573

Get post query via selected category in WordPress

I have a function that gets post in the category selected, it seems to work except for the latest post.

The latest post will not show, it only shows once I publish a new post then the previous will show...?

<?php
global $post;
global $categoryName; //access it through a global variable.
$myposts = get_posts(array(
    'posts_per_page' => 5,
    'offset' => 1,
    'category' => 3 // set cat here
));
echo '<div class="recent-post">';
if ($myposts) {
    foreach ($myposts as $post) :
        setup_postdata($post);
        ?>

        <a class="col-xs-3 col-md-2" href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail('thumbnail'); ?>
        </a>
        <div class="col-xs-3 col-md-10">
            <a class="" href="<?php the_permalink(); ?>">
                <h2><?php the_title(); ?></h2>
            </a>
            <?php the_excerpt() ?>
            <a href="<?php the_permalink(); ?>">Read Full</a>
            <br>
            <a class="" href="<?php comments_link(); ?>">
                <?php comments_number('0 Comments', '1 Comments', '% Comments'); ?>.
            </a>
        </div>
        <hr/>

        <?php
    endforeach;
    wp_reset_postdata();
}
echo '</div>';
?>

Better yet, I have a function that is cleaner, if this function could be altered to include the mypost array and have it set if 'category' parameter is null or empty the show all.

<div class="recent-post">
<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5');
while ($popular->have_posts()) : $popular->the_post(); ?>
    <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('thumbnail'); ?>
    </a>

    <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
    </a>

    <br>
    <a href="<?php comments_link(); ?>">
        <?php comments_number('0 Comments', '1 Comments', '% Comments'); ?>.
    </a>

    <?php the_excerpt() ?>
    <a href="<?php the_permalink(); ?>">Read More</a>
    <hr/>
<?php endwhile; ?>
</div>

Upvotes: 2

Views: 621

Answers (2)

Inzmam ul Hassan
Inzmam ul Hassan

Reputation: 215

The simplest get by category could be like this

$type = $myseriesoption;
$args=array(  'post_type' => $type,  'post_status' => 'publish',  'posts_per_page' => 5,  'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
// your code
}
else
{
$attachments = get_posts( array(
    'post_type'      => 'attachment',
    'posts_per_page' => 500,
    'post_status'    => 'any',
    'post_parent'    => null
) );

if ( $attachments ) {
    foreach ( $attachments as $post ) {
        setup_postdata( $post );
        the_title();
        the_attachment_link( $post->ID, false );
        the_excerpt();
    }
    wp_reset_postdata();
}
}
wp_reset_query();

you can increase or decrease parameters according to your requirements

Upvotes: 1

yivi
yivi

Reputation: 47319

You are passing offset 1 to your get_posts call.

Hence, you are offsetting your results by one, and skipping your latest post.

Remove the offset and you'll be good.

Upvotes: 1

Related Questions