在WooCommerce中请求分类时,得到 "per_page必须在1(含)和100(含)之间"。

问题描述 投票:1回答:2

https:/example.comwp-jsonwcv2productscategories?per_page=150&consumer_key=ck_991580c4b11ae619bef763195f67c311aa2bc&consumer_secret=cs_07cacbb022086cf185beb0145fe0e5db01c57&currentLanguage=en。

   {
        "code": "rest_invalid_param",
        "message": "Invalid parameter(s): per_page",
        "data": {
            "status": 400,
            "params": {
                "per_page": "per_page must be between 1 (inclusive) and 100 (inclusive)"
            }
        }
    }

我试图使用wp JSON API从woo-commerce中获取150个类别。我不能使它100.有什么办法来解决这个问题,使用钩子。

php wordpress woocommerce pagination woocommerce-rest-api
2个回答
0
投票
add_action( 'rest_product_query', 'product_override_per_page' );

/* 
 * params is the query array passed to WP_Query
*/
function product_override_per_page( $params ) {
    if ( isset( $params ) AND isset( $params[ 'posts_per_page' ] ) ) {
        $params[ 'posts_per_page' ] = "200";
    }

return $params;
}

不要错过在钩子中用你的职位类型替换客户。


0
投票

看着这里的文档。https:/developer.wordpress.orgrest-apiusing-the rest-apipagination。

看起来per_page参数可以在1到100之间有任何整数值。所以你的问题的答案是 没有

enter image description here

如果你想硬编码

/**
 * Retrieves the query params for the collections.
 *
 * @since 4.7.0
 *
 * @return array Query parameters for the collection.
 */
public function get_collection_params() {
    return array(
        'context'  => $this->get_context_param(),
        'page'     => array(
            'description'       => __( 'Current page of the collection.' ),
            'type'              => 'integer',
            'default'           => 1,
            'sanitize_callback' => 'absint',
            'validate_callback' => 'rest_validate_request_arg',
            'minimum'           => 1,
        ),
        'per_page' => array(
            'description'       => __( 'Maximum number of items to be returned in result set.' ),
            'type'              => 'integer',
            'default'           => 10,
            'minimum'           => 1,
            'maximum'           => 1000,
            'sanitize_callback' => 'absint',
            'validate_callback' => 'rest_validate_request_arg',
        ),
        'search'   => array(
            'description'       => __( 'Limit results to those matching a string.' ),
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'validate_callback' => 'rest_validate_request_arg',
        ),
    );
}

wp-includesrest-apiendpointsclass-wp-rest-controller.php #L381

© www.soinside.com 2019 - 2024. All rights reserved.