• Resolved otta88sun

    (@otta88sun)


    Hi, i’ve a index.php page were i load different query (the principal and other post query).

    In some query I’ve to set the post_per_page and the post__in parameter, but if setted, seems that wordpress doesn’t care about it.
    Seems that is impossible to rewrite some standard args.
    I did it using query_post(new args), but nothing happen. There is no way to rewrite some custom parameters. Have you any idea why this happen?
    PS: I’ld like to solve this problem without using any add_filter.

    Thank you!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi,

    You can add 'posts_per_page' => -1 to your query_posts like this:

    query_posts( array( 'posts_per_page' => -1) );

    Thanks,
    WPShowCase

    Thread Starter otta88sun

    (@otta88sun)

    yes, i know, but i need to select not infinte post. I need to include only some post ID too.

    Could you send your code to make your request easier to understand?

    If query_post doesn’t work then you might need to set global $post to be query_post like this:
    global $post = query_post( $args );

    Thread Starter otta88sun

    (@otta88sun)

    it doesn’t work. I explain.

    My loop load the most near (20km) geotagged post of today. If there are not enough posts near, then load the not near one to complete the get_option(‘post_per_page’) (set in reading settings).

    So my loop.php do that:

    – get the id of all post near
    – if not enough add other id/ids (more recent)
    – all the id are in array
    – use post__in => the array of id in main loop to alter it, but doesn’t work.
    PS: the array is filled correctly, so is a problem of loop

    $args = array(
    	'post_type' => 'post',
    	'post__in' => $near_post,
    	'category__in' => array(1)
    );
    query_posts($args);
    
    while (have_posts()) : the_post(); ?>
    etc. ....

    If the query is correct then the loop should work.

    If there are not enough posts near, then load the not near one to complete the get_option(‘post_per_page’)

    Are you using a query like this?

    $args = array(
    	'post__in' => $posts,
            'geotag' => 'near'
    );

    Then the two conditions AND together – not OR together. So this would return no posts unless the posts in $posts are also near.

    If that’s not the problem, then posting as much code as possible would be really helpful.

    Thread Starter otta88sun

    (@otta88sun)

    Hi, I means. Now i’ll tell you a thing that will make u scream. The same problem happen with a custom wp_query like this in home page.

    $args2 = array(
    				'post_type' => 'post',
    				'post__not_in' => $near_post,
    				'post_per_page' => $less,
    				'category__in' => array(1)
    			);
    			$my_query2 = new wp_query( $args2 );
    			while( $my_query2->have_posts() ){
    				$my_query2->the_post();
    				$near_post[] = get_the_ID();
    			}

    This query, customized and not related to main loop query, has the same problem: – post_per_page number is related to the main loop query.

    So the problem that i think is that EVERY query loaded (query post o simple wp_query) loaded in index.php (the home page) has some parameters that can’t be overwrite like post_per_page and post__in.

    This is my idea, but it is very strange. However, returning to your post, the array is filled correctly (i printed the value of array). The problem i’m 100% sure is in the home page, where its loop can’t be overwritten.

    Thread Starter otta88sun

    (@otta88sun)

    Means, i think that every loop loaded in the home page have some parameters that can’t be overwritten. Every loop, every kind.

    If i set up a custom wp_query, the problem is the same. Seems that there is a upper “god” query which parameters can’t be rewrite.

    Very strange…. And I’m loosing hairs for this XD

    The following code works:

    $args = array(
    	'post_type' => 'post',
    );
    query_posts($args);
    while (have_posts())  {
      the_post();
      the_title();
    }
    wp_reset_postdata();
    
    $args = array(
      'post__in' => array(1)
    );
    query_posts($args);
    while (have_posts())  {
      the_post();
      the_title();
    }
    wp_reset_postdata();

    You might need to post your code if you’d like someone to fix it.

    Thread Starter otta88sun

    (@otta88sun)

    it doesn’t work, so i post the complete code of loop.php. I remeber the problem i’m asking for: “Some parameters like post__in and post_per_page seems to be overwritten by the main homepage wordpress loop”

    All the code is correct ($near_post has all elements id, the problem is really in the query, but can’t understand what is it)

    <?php
    /*
     * Template - loop.php
     *
     */
    ?>
    <div id="mainloop">
    <?php
    
    $args = array(
    	'post__not_in' => get_option('sticky_posts'),
    	'ignore_sticky_posts'=>1,
    	'category__in' => array(1),
    	'post_type' => 'post'
    );
    
    if ( is_user_logged_in() ): // Utente Loggato?
    	$preferenza_utente = get_user_meta(get_current_user_id(), 'wpcf-priorita', true);
    	$posizione_utente = get_user_meta(get_current_user_id(), 'wpcf-luogo', true);
    	$luogo_lat = get_user_meta(get_current_user_id(), '_luogo_lat', true);
    	$luogo_lng = get_user_meta(get_current_user_id(), '_luogo_lng', true);	// Ottengo Coordinate
    
    	//echo $preferenza_utente.' - '.$posizione_utente.' - '.$luogo_lat.' - '.$luogo_lng;
    
    	if( ($preferenza_utente == 1) && ($posizione_utente != 'ALTRO') && ($luogo_lat != NULL && $luogo_lng != NULL) ): // Utente vuole la priorità e abita in Provincia? Ok, procediamo a cercare gli articoli
    
    		$year = date( 'Y', current_time( 'timestamp', 1 ) );
    		$mont = date( 'n', current_time( 'timestamp', 1 ) );
    		$day = date( 'j', current_time( 'timestamp', 1 ) );
    		$args2 = array(
    			'date_query' => array( // ARTICOLI DI OGGI
    				array(
    					'year'  => $year,
    					'month' => $month,
    					'day'   => 10 //$day
    				)
    			),
    			'post__not_in' => get_option('sticky_posts'),
    			'meta_query' => array(	// CAMPI COORDINATE ESISTENTI
                      array(
                         'key' => '_pronamic_google_maps_latitude',
                         'compare' => 'EXISTS'
                      ),
    				  array(
    					 'key' => '_pronamic_google_maps_longitude',
                         'compare' => 'EXISTS'
    				  )
    			),
    			'category__in' => array(1),
    			'post_type' => 'post'
    		);
    		$my_query2 = new wp_query( $args2 );
    		$near_post = array();
    		while( $my_query2->have_posts() ){
    			$my_query2->the_post();
    			$post_lat = get_post_meta(get_the_ID(),'_pronamic_google_maps_latitude', true);
    			$post_lng = get_post_meta(get_the_ID(),'_pronamic_google_maps_longitude', true);
    
    			$theta = $luogo_lng - $post_lng;
    			$dist = sin(deg2rad($luogo_lat)) * sin(deg2rad($post_lat)) +  cos(deg2rad($luogo_lat)) * cos(deg2rad($post_lat)) * cos(deg2rad($theta));
    			$dist = acos($dist);
    			$dist = rad2deg($dist);
    			$miles = $dist * 60 * 1.1515;
    			$miles *= 1.609344;
    
    			//echo ' - '.get_the_ID().' - '.$miles;
    
    			if($miles <= 20){ // Se il post è vicino ad almeno 20Km
    				$near_post[] = get_the_ID(); // Ottengo lista id
    			}
    		}
    		//foreach($near_post as $posta){ echo ' - '.$posta; }
    		$ppp = get_option('posts_per_page');
    		$count = count($near_post);
    		$less = $ppp - $count;
    		//echo ' - '.$ppp.' - '.$count.' - '.$less;
    		if($count < $ppp){ // Troppo pochi post? Aggiungiamo i mancanti
    			$args2 = array(
    				'post_type' => 'post',
    				'post__not_in' => $near_post,
    				'post_per_page' => $less,
    				'category__in' => array(1)
    			);
    			$my_query2 = new wp_query( $args2 );
    			while( $my_query2->have_posts() ){
    				$my_query2->the_post();
    				$near_post[] = get_the_ID();
    			}
    		}
    		//foreach($near_post as $posta){ echo ' - '.$posta; }
    		$args = array(
    			'post_type' => 'post',
    			'post__in' => $near_post,
    			'category__in' => array(1)
    		);
    
    	endif;
    endif;
    
    query_posts($args);
    $dim = 'medium';
    
    $iloop = 0;
    while (have_posts()) : the_post(); ?>
    	<?php if(has_post_format('status')): ?>
    		<a <?php post_class('loop'); ?> id="post-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" style="<?php echo wepavia_back_type(1); ?>" <?php if($iloop>3){ echo 'title="'.wepavia_mediumbox_title().'"'; }?> >
    			<div>
    				<div class="text"><?php echo get_the_content(); ?></div>
    			</div>
    		</a>
    	<?php elseif(has_post_format('image')): ?>
    		<?php $wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), $dim); //thumbnail ?>
    		<a <?php post_class('loop'); ?> id="post-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" style="<?php echo wepavia_back_type(1); ?>" >
    			<div style="background: url(<?php echo $wpc_image_url[0]; ?>); background-size: cover; background-position: center center; background-repeat: no-repeat;" >
    				<div class="like"><?php echo get_post_meta(get_the_ID(), 'facebook_like_count', true).' like'; ?></div>
    				<div class="text"><?php echo wepavia_preaddcat().get_the_title(); ?></div>
    			</div>
    		</a>
    	<?php else: ?>
    		<?php $wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), $dim); //thumbnail ?>
    		<a <?php post_class('loop'); ?> id="post-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" style="<?php echo wepavia_back_type(1); ?>" <?php if($iloop>=7){ echo 'title="'.wepavia_mediumbox_title().'"'; } ?> >
    			<div style="background: url(<?php echo $wpc_image_url[0]; ?>); background-size: cover; background-position: center center; background-repeat: no-repeat;" >
    				<div class="date"><?php echo get_the_date(); ?><?php echo ' | di '.get_the_author(); ?></div>
    				<h2><?php echo wepavia_preaddcat().get_the_title(); ?> <?php echo wepavia_leggicaso(get_the_ID()); ?></h2>
    				<div class="like"><?php echo get_post_meta(get_the_ID(), 'facebook_like_count', true).' like'; ?></div>
    				<?php echo wepavia_addcontent(); ?>
    				<div class="looptext"><?php echo strip_tags(get_the_content('(continua...)')); ?></div>
    			</div>
    		</a>
    	<?php endif; ?>
    <?php $iloop++; endwhile; ?>
    </div>

    [Moderator Note: No bumping, thank you.]

    Thread Starter otta88sun

    (@otta88sun)

    Hi! I found the solution! Everything is in this topic: Solution

    However my idea is that the index page has some parameters like posts_per_page that can’t be overwrite, a custom wp_query() can’t do it too.

    With post__in the problem was that i’ve had to order the post for the post__in like ‘orderby’ => ‘post__in’, else it show post from the newest to oldest.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Main loop problem parameters’ is closed to new replies.