• Hi, i’ve a custom query in the main page:

    <?php
    $args = array(
    	'post_type' => 'post',
    	'post__in' => get_option('sticky_posts'),
    	'posts_per_page'=>3,
    	'paged' => 1,
    	'order' => 'rand'
    );
    
    $my_query = new WP_Query($args);
    ?>
    <div id="sticky-home">
    	<?php while( $my_query->have_posts() ):
    	$my_query->the_post(); ?>
    		<?php $wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium'); //thumbnail ?>
    		<a class="cbox" id="post-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" style="background: url('<?php echo $wpc_image_url[0]; ?>'); background-size: cover; background-position: center center; background-repeat: no-repeat;" >
    			<h2><?php echo get_the_title(); ?> <?php echo wep_leggicaso(get_the_ID()); ?></h2>
    			<div class="like"><?php echo wep_addlikecount(get_the_ID()); ?> like</div>
    			<?php echo wep_addcontent(); ?>
    			<!--<div class="date"><?php echo get_the_date(); ?><?php echo ' | di '.get_the_author(); ?></div>-->
    			<!--<div class="looptext"><?php echo strip_tags(get_the_content('(continua...)')); ?></div>-->
    		</a>
    <?php  endwhile; ?>
    </div>

    The problem is that the value ‘posts_per_page’ is not correct showed, i mean, the query display more than 3 posts. I really don’t understand where can be the problem. I’ve already deactivated all plugin but nothing change. Any help?

Viewing 6 replies - 1 through 6 (of 6 total)
  • hi

    if posts_per_page does not work it is always worth trying numberposts => 3

    post per page and numberposts are supposed to be interchangeable but sometime I find one works when the other doesn’t.

    not sure why, just one of those things.

    Thread Starter otta88sun

    (@otta88sun)

    Hi, tried but id doesn’t work. This is my first real big problem i faced with WP. In my mind i figure that the main query loop overwrite some parameters of this query. But its strange, i use a custom wp_query…

    hi

    try using global post query. which is the way I go about things, not sure it is the most efficient though.

    global $post;
    $args = array(
    	'post_type' => 'post',
    	'post__in' => get_option('sticky_posts'),
    	'posts_per_page'=>3,
    	'paged' => 1,
    	'order' => 'rand'
    );
    $myposts = get_posts( $args ); ?>
    
    <div id="sticky-home">
    <?php foreach( $myposts as $post ) :	setup_postdata($post); ?>
    
    <?php $wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium'); //thumbnail ?>
    		<a class="cbox" id="post-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" style="background: url('<?php echo $wpc_image_url[0]; ?>'); background-size: cover; background-position: center center; background-repeat: no-repeat;" >
    			<h2><?php echo get_the_title(); ?> <?php echo wep_leggicaso(get_the_ID()); ?></h2>
    			<div class="like"><?php echo wep_addlikecount(get_the_ID()); ?> like</div>
    			<?php echo wep_addcontent(); ?>
    			<!--<div class="date"><?php echo get_the_date(); ?><?php echo ' | di '.get_the_author(); ?></div>-->
    			<!--<div class="looptext"><?php echo strip_tags(get_the_content('(continua...)')); ?></div>-->
    		</a>
    </div>
    
    <?php endforeach; ?>
    <?php wp_reset_query();?>

    Don’t forget to close off the query and reset the main loop at the end.

    Thread Starter otta88sun

    (@otta88sun)

    hi! nice! it works! But is a “official way” to fix it? Or a custom one?
    However my biggest question is always why in a wp_query custom loop so much difficult things…

    Hi
    I’m not sure how you’ve set your page up.

    If you have more than one loop then you need to set up the call in the way I’ve shown. You can really only have one main loop to a page.

    Lots of information on the loop in the codex.

    https://codex.www.ads-software.com/The_Loop

    My guess is that if you are having problems displaying using your main loop it’s because you’ve called it elsewhere on your page but without seeing the context it is difficult to know.

    Thread Starter otta88sun

    (@otta88sun)

    sure i know, I can have only a main loop. But the custom wp_query like $myloopcustom = new WP_Query($argscustom); shouldn’t create problem. That is what I’ve learned until now…. Then there is to see…

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP_Query doesn't care some args’ is closed to new replies.