0

I using plugin advanced custom fields to show recommended posts in my sidebar (posts that authors select as recommended using radio buttons (yes or no)). And it is all working well.

Now, i will need to show that same recommended posts in my mobile application via json.

I found this amazing plugin: https://wordpress.org/plugins/acf-to-rest-api/

Now when i open json at /wp-json/wp/v2/posts i can see field ACF

And there i can see fields recommended_sidebar: yes or recommended_sidebar: no

But this JSON will show all posts (latest posts). Is it possible to make some filter for posts? I will like to show only posts that have recommended_sidebar: yes ? Something like: https://www.domain.com/wp-json/wp/v2/posts?filter[recommended_sidebar]=yes

If is not possible to create that filter via url, then only option is to create custom endpoint. So i create this:

add_action( 'rest_api_init', 'api_hooks' );
function api_hooks() {

   register_rest_route( 'get-post-sidebar/v1', '/go', array(
       'methods'  => 'GET',
       'callback' => 'get_post_sidebar',
   ) );

}
function get_post_sidebar($request_data){
 // $data = $request_data->get_params();


 $data = array();

 $args = array(
 'post_type'   => 'post',
 'post_status' => 'publish',
 'orderby'   => 'id',
 'order'     => 'DESC',
 'meta_query' => array(
   'relation' => 'OR',
     array(
         'key'       => 'recommended_sidebar',
         'value'     => 'yes', 
         'compare'   => '=',
     ),
 ),


 'paged' => 1,
 'posts_per_page' => 2,
 );

 $the_query = new WP_Query( $args );

 while ( $the_query->have_posts() ) {
     $the_query->the_post();
     array_push($data,
         array(
             'title' => get_the_title(),
             'content' => get_the_content(),
             'date' => get_the_date(),
             'number_of_comments' => get_comments_number(),
             'thumbnail' => get_the_post_thumbnail_url()
         )
     );
 }

 wp_reset_postdata();


 $response = new \WP_REST_Response( $data );
 $response->set_status( 200 );

 return $response;
}

This this endpoint will show only sticky posts. Also, it is set to:

 'paged' => 1,
  'posts_per_page' => 2,

But all sticky posts will show up in endpoint.

  • if i change this value to:
 'paged' => 2,
  'posts_per_page' => 2,

Then it will show 2 posts, but that 2 posts are sticky posts :(

  • if i change this value to:
 'paged' => 2,
  'posts_per_page' => 8,

Then it will show 8 that are not recommended_sidebar: yes, but there are not sticky too, it is very very strange.

I will like to show only 8 latest posts that have recommended_sidebar: yes

Best Regards Thank you!

1
  • it is so strange, if i set 'paged' => 1, or 'paged' => 2, it will return totally different posts. And with 'paged' => 1, and 'posts_per_page' => 8, it will show all posts with 'paged' =>2, and 'posts_per_page' => 8, it will show 8 post (as i want) Mar 5 at 13:57
0

Maybe try amending the query to something like this (i havent test this but cant hurt to try) maybe get rid of the meta query itself and just add

'meta_key' => 'recommended_sidebar',
'meta_value' => 'yes'

into the query itself

1
  • Thank you @Stevo I have added meta_ , but it still showing only sticky posts Mar 5 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.