skip to Main Content

https://example.com/wp-json/wc/v2/products/categories/?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)"
            }
        }
    }

I am trying to get the 150 categories from the woo-commerce using the wp JSON API. I can’t make it 100.is there any way to fix this using the hooks.

2

Answers


  1. 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;
    }
    

    Don’t miss to replace customer with your post type in hook

    Login or Signup to reply.
  2. Looking at the documentation here:
    https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

    It looks like the per_page parameter can have any integer value between 1 and 100. So the answer to your question is NO

    enter image description here

    If you want to hard code

    /**
     * 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-includes/rest-api/endpoints/class-wp-rest-controller.php #L381

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search