WP_QUERY -POST_PER_PAGE参数不起作用?

发布于 2025-02-03 20:21:04 字数 1789 浏览 2 评论 0原文

因此,设置工作正常,但是我试图限制显示的标题数量,但没有任何工作。我想念什么?因此,它应限制为10或5个标题。

我尝试了我能想到的一切,但由于某种原因,它没有考虑到限制。

<?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 class="listing-detail custom-scrollbar">
        <?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;?>" class="place-view" data-id="<?php echo $r->ID;?>"><?php echo get_city($r->city_id)->name;?></a></li>
        <?php endforeach;?>
    </ul>
        
    <?php

}else{

    ?>
        <span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
    <?php
}
?>

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 class="listing-detail custom-scrollbar">
        <?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;?>" class="place-view" data-id="<?php echo $r->ID;?>"><?php echo get_city($r->city_id)->name;?></a></li>
        <?php endforeach;?>
    </ul>
        
    <?php

}else{

    ?>
        <span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
    <?php
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2025-02-10 20:21:04

在WP_QUERY实际执行所需的SQL命令之前,有几个过滤器可以运行,这意味着您的post_per_page设置实际上可以在返回结果之前被覆盖。

不幸的是,这可以在您可能使用的主题以及任何可能活动的插件中完成。在这种情况下,我要做的是首先检查我的任何其他代码,以查看我是否正在修改 pre_get_posts post_limits 过滤器。

然后,我会检查主题设置以查看是否具有为此设置的设置,最后一个度假胜地将是一个一个接一个地禁用插件,以查看是否有任何正在过滤查询的插件。

最后,不要打折缓存是一个问题。即使您登录WordPress,有些主机也可以强制迫使页面缓存。

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.

月光色 2025-02-10 20:21:04

我这样纠正了。
因此,您可以采用模式。
让我知道它是不起作用还是
我使用posts_per_page设置每个页面的帖子数(每页标题数),或者您可以使用numberpost
numberPosts用于搜索和显示20个帖子,posts_per_page用于每页显示20个帖子。

$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 class="listing-detail custom-scrollbar">
        <?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; ?>" class="place-view"
                   data-id="<?php echo $r->ID; ?>">
                    <?php echo get_city($r->city_id)->name; ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>

    <?php

} else {

    ?>
    <span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
    <?php
}

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 class="listing-detail custom-scrollbar">
        <?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; ?>" class="place-view"
                   data-id="<?php echo $r->ID; ?>">
                    <?php echo get_city($r->city_id)->name; ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>

    <?php

} else {

    ?>
    <span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
    <?php
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文