简体   繁体   English

'posts_per_page'=> -1不起作用

[英]'posts_per_page' => -1 not working

I would like to run a query through all the posts in the $arg but it is only working for 11 posts. 我想通过$ arg中的所有帖子运行查询,但它仅适用于11个帖子。 I've tried many suggestions from forums but I can't get it to work. 我尝试了来自论坛的许多建议,但无法正常工作。

what do I need to do? 我需要做什么?

function test_update_random_number()
{
    global $post;
    $args    = array (
        'orderby'        => 'meta_value LIKE "%Yes%" rand',
        'order'          => 'DESC',
        'meta_key'       => 'feature_in_search',
        'post_type'      => 'therapist',
        'post_status'    => 'publish',
        'posts_per_page' => '-1'
    );
    $myposts = new WP_Query( $args );
    if ( $myposts->have_posts() ) :
        while ( $myposts->have_posts() ) : $myposts->the_post();

            $value = get_field( "feature_in_search", $post_id );
            if ( strpos( $value, 'es' ) !== false ) {
                $random_value = rand( 1, 100 );
                update_field( "field_58aebd8e060c0", $random_value, $post_id );
            } else {
                $random_value2 = rand( 100, 600 );
                update_field( "field_58aebd8e060c0", $random_value2, $post_id );
            }
        endwhile;
        wp_reset_postdata();
    endif;
}

Your problems is not with the posts_per_page parameter, but with the orderby parameter. 您的问题不在于posts_per_page参数,而在于orderby参数。

You have 'orderby' => 'meta_value LIKE "%Yes%" rand', , which doesn't make much sense. 您有'orderby' => 'meta_value LIKE "%Yes%" rand', ,这没有多大意义。 Also, if you want to order randomly, ordering ASC or DESC doesn't make sense either... :) 另外,如果您想随机订购,订购ASCDESC也没有意义... :)

Apparently you want to filter all the posts that have a meta_field "feature_in_search" with a value of "Yes", and randomly sorted. 显然,您希望过滤所有meta_field为“ feature_in_search”且值为“ Yes”并随机排序的帖子。 So you should do: 因此,您应该执行以下操作:

$args    = [
    'meta_key'       => 'feature_in_search',
    'meta_value'     => 'Yes',
    'post_type'      => 'therapist',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'orderby'        => 'rand'
];

If you really want to search for things that contain "Yes" (and use a "LIKE" search) you would need to construct a proper meta query . 如果您确实想搜索包含 “是”的内容(并使用“喜欢”搜索),则需要构造一个适当的元查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM