I've got such post loop:

global $post;
$post = $post_object;
setup_postdata( $post );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    'post_type'      => 'post', // You can add a custom post type if you like
    'paged'          => $paged,
    'posts_per_page' => 1

if ( have_posts() ) : ?>

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

                                <div class="row single-post">
                                <div class="col-lg-2">
                                        <div class="date"><?php the_time('d.m.Y'); ?></div>
                                <div class="col-lg-9 content">
                                    <?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive post-image")); ?>
                                    <h2><?php echo get_the_title(); ?></h2>
                                    <p class="single-content"><?php the_excerpt(); ?></p>
                                    <div class="post-meta">
                                            <a href="<?php the_permalink(); ?>" title="Czytaj więcej" class="read-more">>> Czytaj więcej..</a>
                                            <p>Kategorie wpisu: <?php $category = get_the_category(); if($category[0]){ echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; }    ?></p>

<?php endwhile; wp_reset_postdata(); ?>

    <div class="center"><?php my_pagination(); ?></div>

<?php else : ?>

        <div class="center">Nope:( no posts yet.</div>

<?php endif; ?>

The question is - can i somehow optimize it? I need to use setup/reset postdata as i use it to generate list of most often viewed posts.. thanks


First of all: do not use query_posts

use get_posts or new WP_Query instead.

If you want to show only most viewed posts, you'll need to add action ( for ex. wp ) check if your on single post page and update that post meta views ( +1 ). Then you could do something like these

$topViewedPosts = new WP_Query( array(
    'post_type' => 'post',
    'meta_key' => 'views',
    'orderby' => 'meta_value_num', // sort by views
    'order' => 'DESC', // sort 3 - 2 - 1
    'posts_per_page' => 5
) );
if( $topViewedPosts->have_posts() )
    while( $topViewedPosts->have_posts() ) : $topViewedPosts->the_post();
        // your content here
endif; wp_reset_postdata();

May be i misunderstood what you'r looking for.. try to explane better than..

  • okey.. so :) query from my code should show all posts. yet.. through setup_postdata and reset_postdata I collect data for other query (which works fine). Ive tried to use get_posts unfortunatelly with that my pagination didnt work at all :| – Paweł Skaba Sep 4 '15 at 6:52
  • So you'r ploblem is with pagination ? try to do wp_reset_postdata() after calling pagination.. as i recall default wp pagination uses global $wp_query; and your resetting it before your pagination uses it ( at least i think so.. ) – Maikal Sep 4 '15 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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