Support » Fixing WordPress » posts_per_page not working with custom post type

  • Resolved TrishaM

    (@trisham)


    I have a custom post type (deal) set up, with custom taxonomy (deals), and a loop in a template file to display 5 links to posts of that custom post type, but it will only show 2 no matter what I do….here’s my loop:

    <?php global $post;
    $today = time('now');
    $args = array(
       'post_type' => 'deal',
       'deal_category' => 'deals',
       'posts_per_page' => 5,
       'meta_key' => 'date_end',
       'orderby' => 'meta_value_num',
       'order' => 'ASC');
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    $exp_date = get_post_meta($post->ID,'date_end',true);
    $expiration_date = strtotime($exp_date);
    
    if ($expiration_date > $today) {
    ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php } endforeach; wp_reset_postdata(); ?>

    I know I have more than two posts in this custom post type with the ‘deals’ category assigned to them, and they are not expired (not past the date_end)…..but I cannot figure out how to make it show more than 2 posts. I even tried “numberposts” instead of “posts_per_page” but that didn’t work.

    This loop is NOT going on a taxonomy page so I don’t think using solutions I’ve found here that others have posted will work as those involved setting a specific number of posts in the functions.php using if_tax()….it’s being used on a page where there are multiple loops already working correctly for other post types.

    Any suggestions?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Well. Nevermind. I think I found the problem and it’s with the Advanced Custom Fields plugin.

    I use ACF to add custom meta-boxes for the custom post type, and apparently if you modify the metadata using the ACF-created metaboxes, it does NOT update the database – when I took a peek at the post_meta table using phpMyAdmin, the incorrect data was still there……

    SO I’ll post this bug in the support threads for that plugin.

    Hi TrishaM,

    I’m having the same issue. Did you find a workaround?

    Hi @jefffassnacht – yes actually I did get this working……..sorry for not posting the solution here, usually I try to do that, but I had started another Post on this issue because it was not really related to the ACF plugin as I had thought in this post…

    So the solution is detailed in my last post here:
    http://wordpress.org/support/topic/custom-post-type-loop-posts_per_page-not-working?replies=7

    Basically I had to change my loop to not use “foreach” and instead go back to the if/while method of looping through posts…..

    So take a look at that post and if it doesn’t help you send me a message through the Forum or Post here again and I’ll try to help you if I can.

    Hi @trisham – Thanks for the update. Here’s another solution I found. Passing the arguments to a custom query variable such as $taxonomy_query instead of the native $query solved my problem. So in the end the code was something like:

    $args = array( [parameters] );
    $taxonomy_query = new WP_Query( $args );
    while ($taxonomy_query->have_posts()) : $taxonomy_query->the_post();

    instead of:

    $args = array( [parameters] );
    $query = new WP_Query( $args );
    while (have_posts()) : the_post();

    This gave me control over the posts_per_page again.

    An interesting solution……although I think the actual key is using the ‘while’ statement in the loop versus the ‘foreach’……I played around with a custom query variable and had the same results, so I just simplified it back to the stock $query….but your method works just as well!

    🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘posts_per_page not working with custom post type’ is closed to new replies.