Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter neonmx

    (@neonmx)

    Thanks!

    I used the information in that ticket and made some modifications for my case, where the “author” and “posts_per_page” parameters weren’t filtering and the query result was all the records.

    In the file: /wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    On line 449 (inside the “get_items” method), after creating the new instance of “WP_Query” and execute the query.

    This modification temporarily resolves the filtering by the “author” and “posts_per_page” parameters sent by the endpoint.

    Ex: {{url}}/wp-json/wp/v2/posts/?per_page=3&author=2

           $args = apply_filters("rest_{$this->post_type}_query", $args, $request);
    $query_args = $this->prepare_items_query($args, $request);

    $posts_query = new WP_Query();
    $query_result = $posts_query->query($query_args);

    // ..................................................
    // ++ Author Filter
    if (isset($request['author']) && count($request['author'])) {
    $new_query_result = [];
    for ($ii = 0; $ii < count($query_result); $ii++) {
    if (in_array($query_result[$ii]->post_author, $request['author'])) {
    $new_query_result[] = $query_result[$ii];
    }
    }
    $query_result = $new_query_result;
    }

    // ++ Limit Filter
    $limit = (int) $posts_query->query_vars['posts_per_page'];
    if (
    $limit > 0 &&
    count($query_result) > $limit
    ) {
    $query_result = array_slice($query_result, 0, $limit);
    }
    // ..................................................

    // Allow access to all password protected posts if the context is edit.
    if ( 'edit' === $request['context'] ) {
    add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    }
    • This reply was modified 1 year, 1 month ago by neonmx.
Viewing 1 replies (of 1 total)