• I’m looking to include a key with each post/page in this resulting query to include the parent id’s but it doesn’t seem t be working I’ve tried doing the things that have been commented out but they weren’t working. I’m looking to try to pass this for a rest api endpoint.

    
    // run query
    	  $search_query = new WP_Query();
    	  $search_query->parse_query( $args );
    	  if ( function_exists( 'relevanssi_do_query' ) ) {
    		relevanssi_do_query( $search_query );
    	  }
      
    	  $controller = new WP_REST_Posts_Controller('post');
    	  $posts = array();
      
    	//   foreach($search_query->posts as $po) {
    	// 	  $po->parent_id = "test";
    	//   }
    
    	  while ( $search_query->have_posts() ) : $search_query->the_post();
    	    // $search_query->post->test = "Test";
    		$data    = $controller->prepare_item_for_response( $search_query->post, $request );
    		$posts[] = $controller->prepare_response_for_collection( $data );
    	  endwhile;
    
    	//   foreach($posts as $post) {
    	// 	  $post['parent_id'] = wp_get_post_parent_id($post->$id) ;
    	//   }
    
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi, @shuangmiles. Do you only need access to the parent ID via the response? The rest response should have a parent ($post->parent) field for each of the post objects, judging by the code here: https://developer.www.ads-software.com/reference/classes/wp_rest_posts_controller/prepare_item_for_response/

    Or, is it a requirement to separate the parent ID in the returned response?

    Thread Starter shuangmiles

    (@shuangmiles)

    Yeah I need it in the rest response. I don’t see any sortof parent id in the objects in the array.

    Do you mind sharing what the response object/array looks like? Not sure if you’re grabbing this via PHP or JS on the other end. I haven’t worked much with the REST API, but I’ll be happy to see if we can figure this out together.

    Also, can you share your full function/method code from above? There might be something that we’re missing outside of the code snippet.

    Thread Starter shuangmiles

    (@shuangmiles)

    Here is the full function

    
    function relevanssi_search_route_callback( WP_REST_Request $request ) {
    
    	$parameters = $request->get_query_params();
      
    	// Force the posts_per_page to be no more than 12
    	if ( isset( $parameters['posts_per_page'] ) && ( (int) $parameters['posts_per_page'] >= 1 && (int) $filter['posts_per_page'] <= 12 ) ) {
    	  $posts_per_page = intval( $parameters['posts_per_page'] );
    	} else {
    	  $posts_per_page = 12;
    	}
    
    	$page_number = isset( $parameters['page'] ) ? intval(intval($parameters['page'])) : 1;
      
    	  // default search args
    	  $args = array(
    		's'               =>  $parameters['s'],
    		'no_found_rows'=> true,
    		'paged' => $page_number  ,
    		'posts_per_page'  =>  $posts_per_page,
    		'post_status' => 'publish',
    		'ignore_sticky_posts' => true,
    		'post_type'        => array('post', 'page')
    		// 'fields' => 'id=>parent'
    	  );
      
    	  // run query
    	  $search_query = new WP_Query();
    	  $search_query->parse_query( $args );
    	  if ( function_exists( 'relevanssi_do_query' ) ) {
    		relevanssi_do_query( $search_query );
    	  }
      
    	  $controller = new WP_REST_Posts_Controller('post');
    	  $posts = array();
      
    	//   foreach($search_query->posts as $po) {
    	// 	  $po->parent_id = &quot;test&quot;;
    	//   }
    
    	  while ( $search_query->have_posts() ) : $search_query->the_post();
    	    // $search_query->post->test = &quot;Test&quot;;
    		$data    = $controller->prepare_item_for_response( $search_query->post, $request );
    		$posts[] = $controller->prepare_response_for_collection( $data );
    	  endwhile;
    
    	//   foreach($posts as $post) {
    	// 	  $post['parent_id'] = wp_get_post_parent_id($post->$id) ;
    	//   }
    
    	  $results = array();
    	  $results['total_results_count'] = $search_query->found_posts;
    	  $results['posts'] = $posts;
      
    	// return results
    	if( ! empty( $posts ) ) {
    	//   return new WP_REST_Response( $posts, 200 );
    	return new WP_REST_Response( $results , 200 );
    	} else {
    	  return new WP_Error( 'No results', 'Nothing found' );
    	}
      
      }
    • This reply was modified 2 years, 6 months ago by Steven Stern (sterndata).
    • This reply was modified 2 years, 6 months ago by bcworkz. Reason: code format fixed

    What does the response look like from the API? I’m assuming you’re getting this via PHP or JavaScript on the other end.

    Thread Starter shuangmiles

    (@shuangmiles)

    I’m using JS, each post/page data is rather large but none of the keys that it has includes parent id or even any of the test keys that I tried inserting through php. I tried to loop through each and assign a new key to equal the result of wp_get_post_parent_id but that doesn’t seem to show up.

    Can you at least provide a sample of the data? I doesn’t have to be the entire object.

    It should look something like:

    
    {
    	"total_results_count": 10,
    	"posts": [
    		{
    			"id": 100
    			...
    		}
    	]
    }
    

    Specifically, I’m trying to figure out what data is appearing for individual post objects.

    Thread Starter shuangmiles

    (@shuangmiles)

    {
    	"total_results_count": 10,
    	"posts": [
    		
    {
          "id": 26397,
          "date": "2020-07-10T12:33:42",
          "date_gmt": "2020-07-10T12:33:42",
          "guid": {
            "rendered": ""
          },
          "modified": "2022-04-19T19:57:14",
          "modified_gmt": "2022-04-19T19:57:14",
          "slug": "video-testimonials",
          "status": "publish",
          "type": "page",
          "link": "https://81acf57d5a.nxcli.net/video-testimonials",
          "title": {
            "rendered": "Video Testimonials"
          },
          "content": {
            "rendered": "",
            "protected": false
          },
          "excerpt": {
            "rendered": "",
            "protected": false
          },
          "author": 1,
          "featured_media": 0,
          "comment_status": "closed",
          "ping_status": "closed",
          "sticky": false,
          "template": "",
          "format": "standard",
          "meta": {
            "inline_featured_image": false
          },
          "categories": [
            
          ],
          "tags": [
            
          ],
          "yoast_head": "",
          
            ]
          }
    	]
    }
    Moderator bcworkz

    (@bcworkz)

    Something has already filtered the response. There are normally “parent” and “menu_order” fields between “featured_media” and “comment_status”.

    Do the deactivate plugins and switch to a default theme debugging procedure. The missing fields should appear. Restore your theme and plugins, one at a time, testing after each. When the fields again disappear, the last activated is the cause.

    You have $post['parent_id'] in commented code. FWIW it should be $post['parent']. There could be a similar error elsewhere, I didn’t look that closely.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Looking to add the parent id to each resulting posts/pages for rest api’ is closed to new replies.