I’m making use of WordPress REST API to fetch data required for running a mobile app.

I have added a new database table which stores user device registration number and post ids when the user saves a post from the mobile app.

When User requests the saved posts from mobile app, I want to list all the posts saved by user by fetching the data from database

Now,

I would like to know how I can add a query var to the existing API call and retrieve the saved posts by user
Currently, the below API query fetched latest 10 posts from blog
http://example.com/wp-json/wp/v2/posts?per_page=10

When I change the API by adding a regid var to the URL, it should fetch the posts that are saved by the specific user having regid 123456

http://example.com/wp-json/wp/v2/posts?per_page=10&regid=123456

How can I add this filter to API?

I’m getting the saved post ids from the table using the below query

$regid = $_REQUEST['regid'] ;
    $sql1 = "SELECT postid FROM ".$save_table." WHERE regid='".$regid."';";
    $result1 = $wpdb->get_results($sql1);

Do I need to create a custom route for this? or I can filter on the existing one?

Read more here: Adding custom query vars to WordPress REST API


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: