0

I have created custom Wordpress loop using WP_Query, loop is working fine and I can customize it nicely, however i have a problem using "Blog pages show at most" settings, for example inside my loop if i have added following argument 'post_per_page' =>-1 my loop is showing all posts but now i can't insert my custom number of posts inside admin panel if I enter "Blog pages show at most" 3 (three representing number of posts) they are ignored and loop is showing all posts.

So to overcome this problem (because i want to control number of posts showing inside admin reading settings)
I have done following : Created a blank value variable and passed it as an argument like this:

$show_my_posts = '';
$args = array(

     'paged' => $paged,
     'post_not_in' => $show_my_posts
     //added blank value variable in order to respect "Blog pages show at most" in backend
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

So now I can insert custom number of posts inside admin backend and my loop is behaving as i want (I enter "Blog pages show at most" and value as 3 ) and now my loop is displaying three posts as I want.

My question is: Am I doing this the "right way" or is there a better way for achieving that behavior? THX!!

My full loop looks like this:

<?php //enable pagination on static pages and blog pages
    $show_my_posts = '';
    $col = 1; //Let's create first column
    /*Let's add pagination to post page and static page*/
    if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) {
    $paged = get_query_var('page');
    } else {
    $paged = 1;
    }

$args = array(
    /* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */    
     'paged' => $paged,
     'post_not_in' => $show_my_posts
             //added blank value variable in order to respect 
            //"Blog pages show at most" in backend
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

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

<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
<?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
    <div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
                <div class="featured_img">
                <?php the_post_thumbnail();
      echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';?>
     </div><!--/featured_img-->
        <?php  // let's enable more link on pages...
        global $more;
        $more = 0;
        ?>
            <?php the_content(); ?> 
            <div class="clear"></div>               
            <div class="custom_fields"><?php the_meta(); ?></div><br/>
            <p class="postmetadata">

            <?php _e('Filed under&#58;','domain'); ?> <?php the_category(', ') ?> <?php _e('by','domain'); ?> <?php  the_author(); ?><br/><?php the_tags('Tags:', ', ', '<br />'); ?>
            <?php _e('Posted on:&nbsp;','domain'); ?><?php the_time('l, F jS, Y'); ?><br/>

            <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?> <?php edit_post_link('Edit', ' &#124; ', ''); ?>
            </p>
        </div>
    </div>

    <?php /*Enable Two Column Layout*/
        if($col==1) {
        $col=2;
        echo "</div>";
        }
        else if($col==2)  {
        $col=1;
         echo "</div>";
        }

    endwhile; ?>
    <div class="clear"></div>
    <div class="navigation">

    <?php
    global $wp_query;
    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, $paged ),
    'total' => $wp_query->max_num_pages
    ) );
    ?>
</div>
<?php endif; ?> 
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
4
  • So nobody knows if I am doing this right or not:(
    – Dejo Dekic
    Commented May 8, 2013 at 21:13
  • What "blank variable" are you referring to? $show_my_posts? What has post_not_in got to do with the number of posts displayed?
    – vancoder
    Commented May 8, 2013 at 21:19
  • Yes i am referring to $show_my_posts,for example if I define post_per_page=>3 my loop will show that number of posts but then I can't change my number of posts in backend (reading settings)...
    – Dejo Dekic
    Commented May 8, 2013 at 21:25
  • 1
    @DejoDekic, please don't pester people for an answer ~10-15 minutes after you post the question.
    – s_ha_dum
    Commented May 8, 2013 at 21:31

2 Answers 2

3

I'm not sure why your blank variable method should work, but it seems dodgy to me. I would explicitly set the posts_per_page to the number defined in settings:

'posts_per_page' => get_option( 'posts_per_page' )
1
  • I knew that my code is "dogy" that is why I asked a question... You did helped me a lot. Thank You!!
    – Dejo Dekic
    Commented May 8, 2013 at 21:43
2

... for example inside my loop if i have added following argument 'post_per_page' =>-1 my loop is showing all posts but now i can't insert my custom number of posts inside admin panel if I enter "Blog pages show at most" 3 (three representing number of posts) they are ignored and loop is showing all posts.

Of course you can't. You have overwritten the admin setting with 'post_per_page' =>-1

But something is odd with your site. If you don't explicitly set posts_per_page WordPress should use the value defined in the backend-- you can see that in the source. WordPress runs the same function call that @vancoder suggests-- $q['posts_per_page'] = get_option('posts_per_page');.

I assume that @vancoder's solution (perfectly fine solution, and I up-voted it) has gotten you around the issue since it is marked "accepted" but it is a work-around to re-enable something that should be happening by default. The real problem is elsewhere, perhaps a poorly written filter or action is too aggressively manipulating queries. I'd bet on pre_get_posts being the hook, but without more information it is impossible to tell.

3
  • I don't know is it related or not but I am using that code inside my custom page template,so if I use my code inside template I need to set get_option('posts_per_page'); and it works, but if I use the same code inside my index.php and omit get_option('posts_per_page') my code works also fine. Is this maybe happening because I am using my code inside a template for some reason?
    – Dejo Dekic
    Commented May 8, 2013 at 22:36
  • I would have to do more experimenting to say for sure but I just did a quick test with several different templates and the code works in all of them.
    – s_ha_dum
    Commented May 8, 2013 at 22:52
  • THX for helping me i really appreciate it...
    – Dejo Dekic
    Commented May 8, 2013 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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