Home > Enterprise >  WP_Query - posts_per_page parameter not working?
WP_Query - posts_per_page parameter not working?

Time:06-03

So the setup is working fine, however I tried to limit the number of titles shown but nothing is working. What am I missing? So it should be limited to 10 or 5 titles.

I tried everything I could think of but for some reason it is not taking the limit in account.

<?php 
$meta_query = array();

$args_booking = array(
    'post_type'      => 'booking',
    'post_status'    => array('publish', 'pending', 'canceled'),
    'nopaging'       => 'false',
    'posts_per_page' => 1,
    'orderby'        => array(
        'menu_order' => 'ASC',
        'date'       => 'DESC',
    ),
);

$meta_query[] = array(
    'key'     => GOLO_METABOX_PREFIX. 'booking_item_author',
    'value'   => $user_id,
    'type'    => 'NUMERIC',
    'compare' => '=',
);

$args_booking['meta_query'] = array(
    'relation' => 'AND',
    $meta_query
);

//$data_booking = new WP_Query($args_booking);
//$total_post = $data_booking->found_posts;

if( count($results) > 0 ){//if( $total_post > 0 ){

    ?>

    <ul >
        <?php foreach ($results as $r):?>
        <?php
            $lang = $r->lang!='nl'?'/' . $r->lang : '';
            $param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
            
            $url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
        ?>
        <li><a href="<?php echo $url;?>"  data-id="<?php echo $r->ID;?>"><?php echo get_city($r->city_id)->name;?></a></li>
        <?php endforeach;?>
    </ul>
        
    <?php

}else{

    ?>
        <span ><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
    <?php
}
?>

CodePudding user response:

There are a couple of filters that run before WP_Query actually executes the desired SQL command, which means you posts_per_page setting can actually get overridden before your results are returned.

Unfortunately this can be done in both the theme you may be using, and in any plugins that maybe active. What I would do in this situation, is first check any of my other code to see if I am modifying the pre_get_posts or the post_limits filter.

I'd then check the theme settings to see if it has a setting for this, and last resort would be to disable plugins one by one to see if any of those are filtering the query.

Lastly don't discount cache being an issue. Some hosts can force caching of pages, even when you are logged in to WordPress.

CodePudding user response:

I corrected this like this. So you can take a pattern. Let me know if it does not work or does I used posts_per_page to set the number of posts per page (number of titles per page) Or you can use numberposts. numberposts is used to search and display only 20 posts and posts_per_page is used to display 20 posts per page.

$args_booking = array(
    'post_type' => 'booking',
    'post_status' => array('publish', 'pending', 'canceled'),
    'nopaging' => 'false',
    'posts_per_page' => 20, // for set limit.
    'orderby' => array(
        'menu_order' => 'ASC',
        'date' => 'DESC',
    ),
    'meta_query' => array(
        array(
            'key' => GOLO_METABOX_PREFIX . 'booking_item_author',
            'value' => $user_id,
            'type' => 'NUMERIC',
            'compare' => '=',
        )
    )
);


$query = new WP_Query($args_booking);
$results = $query->get_posts();


if (count($results) > 0) {//if( $total_post > 0 ){

    ?>

    <ul >
        <?php foreach ($results as $r): ?>
            <?php
            $lang = $r->lang != 'nl' ? '/' . $r->lang : '';
            $param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);

            $url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
            ?>
            <li>
                <a href="<?php echo $url; ?>" 
                   data-id="<?php echo $r->ID; ?>">
                    <?php echo get_city($r->city_id)->name; ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>

    <?php

} else {

    ?>
    <span ><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
    <?php
}
  • Related