Woocommerce Rest APi get custom meta field - woocommerce

I'm trying to pull a products custom meta and custom taxonomy value from woocommerce using the rest api. But no matter what I try a can't seem to get it to work. Not sure what I'm missing.
Example, my products have a custom meta value "product_location". The post meta value exists and works because I can use this to pull the value into the woocommerce product page:
get_post_meta($product->get_id(), 'product_location', true );
In my functions.php file I've added:
add_action( 'rest_api_init', 'handle_location' );
function handle_location() {
register_rest_field( 'post', 'product_location', array(
'get_callback' => array( $this, 'get_the_product_location' ),
'schema' => null
));
}
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], $field_name, true );
}
However, when I make a call say https://*******.com/wc-api/v2/products/302341/ the meta data isn't included. So basically how can I include the additional meta data? I plan on trying this for a custom taxonomy as well. Any help is appreciated - Thanks

Try changing your bottom function to
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], 'product_location', $meta_value );
}

Related

Wordpress REST response shows taxonomy categories only with ID's

I'm using WP as a headless CMS with ACF en ACF-2-REST plugins. I've added categories to a post-type and when I make a GET call, it shows me all the information of a particular post including the categories, but only the ID's. If I want to match it, I have to do another call to Categories to get the information of that categories (name, parent etc).
How can I show that information instead of just the ID in a post call?
How the JSON looks now at the /activities call:
{
"id":111,
"date":"2020-01-18T15:39:27",
"date_gmt":"2020-01-18T15:39:27",
"guid":{"rendered":"https:\/\/url.be\/?post_type=activities&p=111"},
"modified":"2020-01-18T15:39:27",
"modified_gmt":"2020-01-18T15:39:27",
"slug":"walking-on-wood",
"status":"publish",
"type":"activities",
"link":"https:\/\/url.be\/activities\/walking-on-wood\/",
"title":{"rendered":"Walking on wood"},
"template":"",
"categories":[14,25,13,2,18,21,6,24],
"acf":{...}
}
What I want to show in the "categories" instead of just the numbers (from the categories call)
{
"id":3,
"count":1,
"description":"",
"link":"https:\/\/url.be\/category\/duration\/lower-than-30-min\/",
"name":"< 30 min.",
"slug":"lower-than-30-min",
"taxonomy":"category",
"parent":2,"meta":[],
"acf":[],
"_links":{"self":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/3"}],
"collection":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories"}],
"about":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/taxonomies\/category"}],
"up":[{"embeddable":true,"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/2"}],
"wp:post_type":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/posts?categories=3"},{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/activities?categories=3"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}
}
Can't find any solution on the internet how I could manipulate the structure of that JSON with a custom function, would appreciate it a lot if someone point me to the right direction. Thanks!
As discussed in the comments section, a solution to this question is to use a custom endpoint method for the WP REST API and perform extra queries in there to get the data you need. This way, you can do all the data manipulation to get the perfect response, resulting in one REST call.
As taken from the official developer docs
Define an endpoint method and add some extra data
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func', //note that this is the method it will fire
'args' => array(
'id' => array(
'validate_callback' => function($param, $request, $key) {
return is_numeric( $param );
}
),
),
) );
/**
* Grab latest post by an author along with its category!
*
* #param array $data Options for the function.
* #return array Post,
*/
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if(!empty($posts)) {
//Example of appending extra data
foreach($posts as $post) {
$category = wp_get_post_terms($post->ID, 'category');
$post['category'] = $category;
}
return $posts;
} else {
return new WP_Error( 'no_author', 'Invalid author', array( 'status' => 404 ) );
}
}

Wordpress REST API reading post meta but not updating

I am trying to create a PowerApp in which I want to integrate the post meta fields. I inserted this code in the functions.php file. But I can read and write the data for default WordPress fields, but I am not able to update the post custom fields.
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'post', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'update_callback' => 'get_post_meta_update_for_api',
'schema' => null,
)
);
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id );
}
function get_post_meta_update_for_api( $value, $object, $field_name ) {
return update_post_meta( $object[ 'id' ], $field_name, $value );
}

Wordpress API V2 custom fields with custom post types

I'm trying to add my custom posts to the Wordpress API, which also have custom fields.
I can view the custom posts types on the API via, /wp-json/wp/v2/fruit.
But the custom fields aren't displaying, how do you add them to the API?
Take a look at "Extending the REST API / Modifying Responses", and in particular at register_rest_field.
Check out this example:
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'post', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id );
}

Posts from category not displaying when searching for category name

I'm having trouble with my search results page in that it is not displaying posts that are a part of a category when searching for the category name. For Instance, If I search for "doors" (which is a cat) all Partners that are in the "doors" category should be displayed in the search results. Right now, only partners that have the word "doors" in their title or content is displayed.
I'm running a searchAll function so the the standard wp search will search everything.
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'the_search_query', 'searchAll' );
What am I missing?
your query is seraching for post_type, not category_name.
post_type is used for custom post types or taxonomies ..
your query should contain $query->set( 'category_name', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
however, in some cases (and I do not know the reason) that would not work for sub-categories.
in that case, you should use category-slug (slug) insted.
Ive changed my string to this:
<?php
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'category_name', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'pre_get_posts', 'searchAll' );
I do have custom taxonomies though. Basically I'm trying to create a "Search everything" function.

WordPress: Add action for each custom post type

Im looking to add an action for each of the custom post types registered on my WordPress site (when a custom post type is published).
Now I could just manually add one for each of the types I have like:
add_action( 'publish_book', 'function_title_to_call' );
add_action( 'publish_movie', 'function_title_to_call' );
...
But I wanted to ask if there is a way it could be done so its all automated?
I tried the following without luck:
$arguments = array(
'public' => true,
'_builtin' => false
);
$all_post_types = get_post_types( $arguments, 'names', 'and' );
foreach ( $all_post_types as $one_post_type ) {
add_action( 'publish_' . $one_post_type, 'function_title_to_call' );
}
Any suggestions?

Resources