Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need help with loop => Need paged & count increase WordPress

  • SOLVED

I've got a loop pulling in a custom post type.

Right now it's only pulling them into one page, and it's stopping after 11, even though my posts per page is set to 100.

I need it to be paged, and also figure out why the count per page isn't working right.

Here's the code:


<?php

$args = array(
'numberposts' => -1,
'post_type' => 'team-member',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 100

);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

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

<!-- this is my content -->

<?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

<?php endwhile; ?>


Let me know if anything doesn't make sense. Thanks!

Answers (4)

2015-09-17

Andrea P answers:

in theory the numberposts should be not considered, as it isn't a parameter for wp query (it was many years ago but it's deprecated..)

try changing your args with this:


$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'team-member',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 100,
'paged' => $paged
);


but now I am seeing that you set to order by a meta value, but you haven't specified a 'meta_key' which would be the name of the meta field which you want to order by..
possibly the query is crashing after that, and not considering the other parameters you added after that orderby..


p.s. then you can check this code to make the pagination links
https://codex.wordpress.org/Function_Reference/paginate_links#Example_With_a_Custom_Query


Kyler Boudreau comments:

Hey Andrea,

You're the only one of the above who has referenced my request for pagination. Here's my full code as it stands... I'm not a PHP guy. So if you can tell me exactly that to change and paste back into my page, the pot goes to you.



<?php

$args = array(
'numberposts' => -1,
'post_type' => 'team-member',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 100

);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

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

<div class="team-result-box box-sizing fill shadow">
<div class="team-result-profile">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><img src="<?php the_field('profile_pic'); ?>" alt="<?php the_author_firstname(); ?> <?php the_author_lastname(); ?>"></a>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><div class="team-result-button-mobile">View Page</div></a>
</div>

<div class="team-result-col1 box-sizing">

<div class="team-result-name-location">
<div class="team-result-name"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><?php the_author_firstname(); ?> <?php the_author_lastname(); ?></a></div>
<div class="team-result-location"><?php the_field('city'); ?>, <?php the_field('state'); ?></div>
</div>

<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><div class="team-result-button">View Page</div></a>

<div class="clear"></div>
<div class="team-result-social"><?php get_template_part('templates/result-badges'); ?></div>
</div>

</div>

<?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

<?php endwhile; ?>



Andrea P comments:

Hello,

I have omitted the orderby metafield, because I think that was crashing the query and not considering the parameters which were following it (so your posts_per_pages was not considered and defaulting to wordpress settings).

if you want to use 'orderby' => 'meta_value', you also have to specify the slug of the meta field, adding both these:

'meta_key' =>'YOUR_META_FIELD_SLUG',
'orderby' => 'meta_value',

also, if the value of the meta field is numeric, you must use 'meta_key_num' instead of 'meta_key'.

this code is not considering the ordering, but should work and has pagination :


<?php

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'team-member',
'order' => 'ASC',
'posts_per_page' => 100 ,
'paged' => $paged

);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

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

<div class="team-result-box box-sizing fill shadow">
<div class="team-result-profile">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><img src="<?php the_field('profile_pic'); ?>" alt="<?php the_author_firstname(); ?> <?php the_author_lastname(); ?>"></a>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><div class="team-result-button-mobile">View Page</div></a>
</div>

<div class="team-result-col1 box-sizing">

<div class="team-result-name-location">
<div class="team-result-name"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><?php the_author_firstname(); ?> <?php the_author_lastname(); ?></a></div>
<div class="team-result-location"><?php the_field('city'); ?>, <?php the_field('state'); ?></div>
</div>

<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_author_firstname(); ?> <?php the_author_lastname(); ?>" style="color:#000;"><div class="team-result-button">View Page</div></a>

<div class="clear"></div>
<div class="team-result-social"><?php get_template_part('templates/result-badges'); ?></div>
</div>

</div>

<?php endwhile; ?>

<?php
$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, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>

<?php endif; ?>

<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>


Kyler Boudreau comments:

Andrea,

My page white screens with that. I cleared all of the old out and place your code in. Sorry.


Andrea P comments:

mmm..

that's weird because it seems all fine to me.. was it working with your previous code?

the only thing I am seeing is that you are using a couple of function which are deprecated:
the_author_firstname()
the_author_lastname()

you should replace them with:
the_author_meta('first_name')
the_author_meta('last_name')

but at the moment I can't see anything within that code which could crash the page..
is there any other code before or after?


Kyler Boudreau comments:

I'll try it again Andrea. Maybe I did something dumb. The code was and is working before, so it isn't an existing issue. I'll let you know. And thanks for the depreciated code update.


Kyler Boudreau comments:

Yeah, still a no go for me. I updated the depreciated items and that worked fine, but then when I try to implement your other code, it white screens.


Kyler Boudreau comments:

Andrea,

I went through it in pieces to see where the white screen happened. It was the:

<?php if( $the_query->have_posts() ): ?>

line of code. I removed that and the corresponding PHP close tag, and it works great. Thank you!

2015-09-17

Arnav Joy answers:

try this

<?php



$args = array(



'post_type' => 'team-member',

'orderby' => 'meta_value',

'order' => 'ASC',

'posts_per_page' => 100



);



// get results

$the_query = new WP_Query( $args );



// The Loop

?>

<?php if( $the_query->have_posts() ): ?>



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



<!-- this is my content -->



<?php endwhile; ?>



<?php endif; ?>



<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>



<?php endwhile; ?>


Kyler Boudreau comments:

Hey Arnav - tried that but it didn't change anything.

2015-09-17

Sébastien | French WordpressDesigner answers:

there are two times <?php endwhile; ?>


Sébastien | French WordpressDesigner comments:

are you sure you have more than 11 posts ?


Sébastien | French WordpressDesigner comments:

more than 11 "team-member" posts ?


Kyler Boudreau comments:

Positive. I have 34.


Sébastien | French WordpressDesigner comments:

<?php

$args = array(
'post_type' => 'team-member',
'order' => 'ASC',
'posts_per_page' => 100

);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- this is my content -->
<?php the_title() ?>
<?php endwhile; ?>

<?php endif; ?>

2015-09-17

dimadin answers:

'numberposts' is used only if 'posts_per_page' isn't so it is expected that nothing changes.

I actually tried you code and it works as expected (though you have unneeded <?php endwhile; ?> at the end). You should check your error is there some that prevents this or is there some filter that is used and modifies query or number of displayed posts.


dimadin comments:

To see number of requested posts, you can display you query like


add_filter( 'posts_request', 'md_print_r_sql', 999 );
function md_print_r_sql( $sql ) {
print_r( $sql );
return $sql;
}


or this only for limits:



add_filter( 'post_limits_request', 'md_print_r_sql', 999 );
function md_print_r_sql( $sql ) {
print_r( $sql );
return $sql;
}