karannagupta/wordpress-vuejs-search

Term_links, Terms, Custom_excerpt return empty

Closed this issue · 1 comments

WpPosts vue_meta term links, terms and excerpts are empty and displayed posts are all uncategorized even though they have associated taxonomy of terms and categories. At first get_term_link was returning empty and producing a cannot convert to string error, but I managed to solve that by using $term_data->slug instead of name. A var_dump of $additional_post_data returns the correct info when looking at a post in the editor:

(size=5)
'custom_excerpt' => string 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam lobortis magna ut tellus vehicula malesuada eu eget turpis. Lorem ipsum dolor sit amet, consectetur adipiscing …' (length=182)
'terms' =>
array (size=2)
0 => string 'design' (length=6)
1 => string 'dev' (length=3)
'term_links' =>
array (size=2)
0 => string 'design' (length=54)
1 => string 'dev' (length=48)
'featuredmedia_alt' => string 'Beachy City' (length=11)
'featuredmedia_url' => string '//localhost:3002/app/uploads/2020/05/d8db3e70-f8c0-383b-9c7c-2f23a8031043.jpg' (length=77)

The array on the post editor page contains the term_links, terms, and custom_excerpt.

When looking in postman I get a lot of errors for extend-api.php on lines 45 and 60.

/wp-json/wp/v2/posts?per_page=100&page=1&_fields=id,title,link,vue_meta

Notice: Undefined index: categories in /var/www/public/wp-content/themes/vuetwentyseventeen/includes/extend-api.php on line 45

Warning: Invalid argument supplied for foreach() in /var/www/public/wp-content/themes/vuetwentyseventeen/includes/extend-api.php on line 45

Looks like it is having issues with the forEach loop
Further down the response in postman There are cannot modify header information headers already sent errors. I don't know if the data is available at the time it starts the loop and the excerpt property seems to also be unavailable, but for some reason the image data for the post is available

Any help in resolving this would be appreciated. Thank you.

Warning: Cannot modify header information - headers already sent by (output started at /var/www/public/wp-content/themes/vuetwentyseventeen/includes/extend-api.php:45) in /var/www/public/wp-includes/rest-api/class-wp-rest-server.php on line 1337

I figured out what was happening.

Since excerpt and categories didn't exist in the 'fields' variable for the response there was nothing for register_rest_field callback to loop or to get from excerpt. I didn't realize it used the post object data from the fetch call and assumed it grabbed the needed info from the database. I assumed it would exist in the post object because it used it for vue_meta. I'm fairly new to using wp rest api.

Updated fields variable to include the needed info for vue_meta.

This solved it for me: const fields = 'id,title,link,categories,excerpt,vue_meta';

Thank you and my bad lol