Add custom taxonomy term in woocommerce REST API - wordpress

I have created custom taxonomy "Brands" and attached to woocommerce products. Unfortunately, It is not available in woocommerce REST API response.
How to attach custom taxonomy term to woocommerce rest API. Currently there is no documentation about attachment of custom taxonomy. Is there any hook or filter ?

I solved by using this code.You will be able to GET and Update custom taxonomy by using this method. Basically. You can add this code in functions.php file or in plugin.
Step:1
Replace brands with your taxonomy_name.
Step:2
If your taxonomy have custom fields, replace custom_field_name with yours.
Taxonomy Code:
add_action( 'init', 'create_brands_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function create_brands_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Brands', 'taxonomy general name' ),
'singular_name' => _x( 'Brand', 'taxonomy singular name' ),
'search_items' => __( 'Search Brands' ),
'all_items' => __( 'All Brands' ),
'parent_item' => __( 'Parent Brand' ),
'parent_item_colon' => __( 'Parent Brand:' ),
'edit_item' => __( 'Edit Brand' ),
'update_item' => __( 'Update Brand' ),
'add_new_item' => __( 'Add New Brand' ),
'new_item_name' => __( 'New Brand Name' ),
'menu_name' => __( 'Brands' ),
);
$capabilities = array(
'manage_terms' => 'manage_woocommerce',
'edit_terms' => 'manage_woocommerce',
'delete_terms' => 'manage_woocommerce',
'assign_terms' => 'manage_woocommerce',
);
// Now register the taxonomy
$args = array(
'labels' => $labels,
'show_in_rest' => true,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'capabilities' => $capabilities,
);
register_taxonomy( 'brands', array( 'product' ), $args );
register_taxonomy_for_object_type( 'brands', 'product' );
}
Register Taxonomy API for WC
//Register taxonomy API for WC
add_action( 'rest_api_init', 'register_rest_field_for_custom_taxonomy_brands' );
function register_rest_field_for_custom_taxonomy_brands() {
register_rest_field('product', "brands", array(
'get_callback' => 'product_get_callback',
'update_callback' => 'product_update_callback',
'schema' => null,
));
}
//Get Taxonomy record in wc REST API
function product_get_callback($post, $attr, $request, $object_type)
{
$terms = array();
// Get terms
foreach (wp_get_post_terms( $post[ 'id' ],'brands') as $term) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
'custom_field_name' => get_term_meta($term->term_id, 'custom_field_name', true)
);
}
return $terms;
}
//Update Taxonomy record in wc REST API
function product_update_callback($values, $post, $attr, $request, $object_type)
{
// Post ID
$postId = $post->get_id();
//Example: $values = [2,4,3];
// Set terms
wp_set_object_terms( $postId, $values , 'brands');
}

Solution above is working but you have to change the
$postId = $post->get_id();
with
$postId = $post->ID;

Took me ages to find out why update_callback did not work : $values contains the taxonomy-structure and is therefore too complex to pass as integer to wp_set_object_terms. You have to strip it to the numeric ID's only otherwise [null] is assigned
//Update Taxonomy record in wc REST API
function product_update_callback_Leerjaar($values, $post, $attr, $request, $object_type)
{
// Post ID
$postId = $post->id;
//Example: $values = [2,4,3];
error_log("debug on values");
error_log(json_encode($values));
$numarray = [];
foreach($values as $value){
$numarray[] = (int)$value['id'];
}
wp_set_object_terms( $postId, $numarray , 'brands');
}

If anyone has tested all the above and can't get it to work, I found a simplest and working solution, based on Taha Farooqui's answer and the wordpress register_rest_field documentation :
add_action( 'rest_api_init', 'register_rest_field_for_custom_taxonomy_brands');
function register_rest_field_for_custom_taxonomy_brands() {
register_rest_field('product', "field_rest_name",
array("get_callback" => function ($post) {
$taxonomy = wp_get_post_terms( $post['id'], 'your_taxonomy_name');
return $taxonomy;
}
)
);
}

After months of testing trying to prove why the product_update_callback() function returned a null value, he got a version that loads the taxonomy values ​​from the woocommerce Rest Api v3 only with the name and slug..
<?php
//Prevent a malicious user from executing php code from the browser bar
defined('ABSPATH') or die( "Bye bye" );
add_action( 'init', 'create_brands_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function create_brands_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
$labels = array(
'name' => _x( 'Marcas', 'taxonomy general name' ),
'singular_name' => _x( 'Marca', 'taxonomy singular name' ),
'search_items' => __( 'Buscar Marcas' ),
'all_items' => __( 'Todas las Marcas' ),
'parent_item' => __( 'Marca Relacionada' ),
'parent_item_colon' => __( 'Marca Relacionada:' ),
'edit_item' => __( 'Editar Marca' ),
'update_item' => __( 'Subir Marca' ),
'add_new_item' => __( 'Añadir Nueva Marca' ),
'new_item_name' => __( 'Nuevo Nombre de Marca' ),
'menu_name' => __( 'Marcas' ),
);
$capabilities = array(
'manage_terms' => 'manage_woocommerce',
'edit_terms' => 'manage_woocommerce',
'delete_terms' => 'manage_woocommerce',
'assign_terms' => 'manage_woocommerce',
);
// Now register the taxonomy
$args = array(
'labels' => $labels,
'show_in_rest' => true,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'capabilities' => $capabilities,
);
register_taxonomy( 'pwb-brand', array( 'product' ), $args );
register_taxonomy_for_object_type( 'pwb-brand', 'product' );
}
//Register taxonomy API for WC
add_action( 'rest_api_init', 'register_rest_field_for_custom_taxonomy_brands' );
function register_rest_field_for_custom_taxonomy_brands() {
register_rest_field('product', "pwb-brand", array(
'get_callback' => 'product_get_callback_brand',
'update_callback' => 'product_update_callback_brand',
'schema' => null,
));
}
//Get Taxonomy record in wc REST API
function product_get_callback_brand($post, $attr, $request, $object_type){
$terms = array();
// Get terms
foreach (wp_get_post_terms( $post[ 'id' ],'pwb-brand') as $term) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
);
}
return $terms;
}
//Update Taxonomy record in wc REST API
function product_update_callback_brand($values, $post, $attr, $request, $object_type){
// Post ID
$postId = $post->id;
$terms = $values;
$termName = $terms[0]['name'];
$termSlug = $terms[0]['slug'];
wp_insert_term( $termName, 'pwb-brand', array( 'slug' => $termSlug ) );
error_log("debug on values");
error_log(json_encode($values));
$newTermId = get_term_by('slug',$termSlug,'pwb-brand')->term_id;
array_push($values, array('id' => (int)$newTermId));
$numarray = [];
foreach($values as $value){
if(is_numeric($value['id'])){
$numarray[] = (int)$value['id'];
}
}
wp_set_object_terms( $postId, $numarray , 'pwb-brand');
}
After so much testing he understood that the values ​​that pass through the product_get_callback_brand function are mandatory to register a taxonomy so if you add a custom field like in the first example then they must be loaded so that the taxonomy can be retrieved.
It is worth noting that it is not a valid field for image fields or galleries that will not have the permissions to be loaded by the Rest Api v3 of woocommerce

Related

How To Modify Show UI of a Registered Taxonomy On Demand

As you you know the 'show_ui' Boolean option in taking care of rendering or not rending the Taxonomy Menu on UI on registering ataxonomy.
function custom_taxonomy() {
$labels = array(
'name' => 'Taxonomies',
'singular_name' => 'Taxonomy',
'menu_name' => 'Taxonomy',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'view_item' => 'View Item',
'separate_items_with_commas' => 'Separate items with commas',
'add_or_remove_items' => 'Add or remove items',
'choose_from_most_used' => 'Choose from the most used',
'popular_items' => 'Popular Items',
'search_items' => 'Search Items',
'not_found' => 'Not Found',
'no_terms' => 'No items',
'items_list' => 'Items list',
'items_list_navigation' => 'Items list navigation',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'taxonomy', array( 'post' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
Is there any way to modify this option after registering the taxonomy? like any hook or filter to toggle the Boolean in functions.php
You can use register_taxonomy_args to filter the taxonomy options before the taxonomy is registered, or while it's being registered.
Example code:
/*
* #param array $args The taxonomy args such as show_ui.
* #param string $taxonomy The taxonomy name.
*/
add_filter( 'register_taxonomy_args', function ( $args, $taxonomy ) {
if ( 'my_taxonomy' === $taxonomy ) {
$args['show_ui'] = false;
}
return $args;
}, 10, 2 );
There is also an "action" you can "hook" into, where this filter is fired just after a taxonomy is registered. Here, you can, for example, assign other post/object type(s) to the taxonomy.
Example code:
/*
* #param string $taxonomy The taxonomy name.
*/
add_action( 'registered_taxonomy', function ( $taxonomy ) {
if ( 'my_taxonomy' === $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, 'post' );
}
} );
And if you must (or need to) modify the show_ui (or any other options) of a taxonomy after it's registered, then you can use the global $wp_taxonomies, which is an array of all registered taxonomies.
Example code:
/*
* #param string $taxonomy The taxonomy name.
*/
add_action( 'registered_taxonomy', function ( $taxonomy ) {
if ( 'my_taxonomy' === $taxonomy ) {
global $wp_taxonomies;
if ( ! is_array( $wp_taxonomies )
|| ! isset( $wp_taxonomies[ $taxonomy ] ) ) {
return;
}
$wp_taxonomies[ $taxonomy ]->show_ui = false;
}
} );

WordPress Custom Type append Post ID at the end

I've been searching for two days know and tried almost every example I've seen online but the real problem is that all examples have different post-type-names or want different results in general.
That's why I want to ask for help and start from scratch.
I've created a Custom Post Type for listing some Jobs opportunities but because most job-titles have the same name (product designer) I would like to prevent WordPress from adding -1, -2, -3, -4 after each slug over time and that's why I was thinking about adding the %post_id% behind the %post-name% for only this new custom post type.
A post_id should be an unique number so that would prevent ugly urls in the long run.
This is what I have, only the post type setup, but right now I think I need to work with rewrites and stuff.
function jobs() {
$labels = array(
'name' => _x( 'Jobs', 'Post Type General Name', 'theme-name' ),
'singular_name' => _x( 'Job', 'Post Type Singular Name', 'theme-name' ),
'menu_name' => __( 'Jobs', 'theme-name' ),
'parent_item_colon' => __( 'Parent Job:', 'theme-name' ),
'all_items' => __( 'All Jobs', 'theme-name' ),
'view_item' => __( 'View Job', 'theme-name' ),
'add_new_item' => __( 'Add New Job', 'theme-name' ),
'add_new' => __( 'Add New', 'theme-name' ),
'edit_item' => __( 'Edit Job', 'theme-name' ),
'update_item' => __( 'Update Job', 'theme-name' ),
'search_items' => __( 'Search Job', 'theme-name' ),
'not_found' => __( 'Not found', 'theme-name' ),
'not_found_in_trash' => __( 'Not found in Trash', 'theme-name' ),
);
$rewrite = array(
'slug' => 'vacatures',
'with_front' => false,
'pages' => false,
'feeds' => false,
);
$args = array(
'label' => __( 'vacatures', 'theme-name' ),
'description' => __( 'Post Type Description', 'theme-name' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'menu_position' => 9,
'menu_icon' => 'dashicons-welcome-learn-more',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type( 'vacatures', $args );
}
// Hook into the 'init' action
add_action( 'init', 'jobs', 0 );
any help would be so thankful!
EDIT
I'm a bit further now and I have a feeling I'm getting close to what I need.
I've added the code below and the result is almost good. It appends the post_id on the end of the slug only problem it's twice/double.
I want this:
http://example.com/vacatures/product-designer/123/
But instead it does this:
http://example.com/vacatures/product-designer/123/123/
function fix_permalink( $post_link, $id = 0 ) {
add_rewrite_rule(
'^vacatures/',
'index.php?post_type=vacatures&p=',
'top'
);
$post = get_post($id);
if ( is_wp_error($post) || $post->post_type != 'vacatures' ) {
return $post_link;
}
empty ( $post->slug )
and $post->slug = sanitize_title_with_dashes( $post->post_title );
return home_url( user_trailingslashit( "vacatures/$post->slug/$post->ID" ) );
}
add_filter( 'post_type_link', 'fix_permalink' );
Looking at your code, it looks as though you've rewritten the slug to imitate the post ID and yet in the last line of code you state the new permalink should be the post_type followed by the slug (which is now the ID) followed by the ID (the reason for the double).
I did a few tests and indeed either removing the slug or ID from the new permalink structure it did remove the duplicate. But I could not get the_permalink() to correctly link to a single view of my custom post type post (Eg. within an archive page using 'the_permalink()' did not link to my single.php for that post type), so I cannot say that I could get your code to work for me.
But I have been using this snippet for a while and it does what you'd like.
Your rewrite:
'rewrite' => array( 'slug' => 'vacatures' )
The function:
// Rewrite permalink structure
function vacatures_rewrite() {
global $wp_rewrite;
$queryarg = 'post_type=vacatures&p=';
$wp_rewrite->add_rewrite_tag( '%cpt_id%', '([^/]+)', $queryarg );
$wp_rewrite->add_permastruct( 'vacatures', '/vacatures/%cpt_id%/', false );
}
add_action( 'init', 'vacatures_rewrite' );
function vacatures_permalink( $post_link, $id = 0, $leavename ) {
global $wp_rewrite;
$post = &get_post( $id );
if ( is_wp_error( $post ) )
return $post;
$newlink = $wp_rewrite->get_extra_permastruct( 'vacatures' );
$newlink = str_replace( '%cpt_id%', $post->ID, $newlink );
$newlink = home_url( user_trailingslashit( $newlink ) );
return $newlink;
}
add_filter('post_type_link', 'vacatures_permalink', 1, 3);
New permalinks structure is: http://domain.com/vacatures/208/
I hope this helps you.

Wordpress: pre_get_posts and order by taxonomy term

I have a category archive with a custom taxonomy "client". Now I want to order the category archive by the term of the custom taxonomy (the client name). I tried to query it by meta_value and a advanced custom taxonomy query, but I could not get it to be ordered by the name of the taxonomy term. Any suggestions to get a Wordpress loop ordered by taxonomy term name? See line $query->set( 'orderby', 'how_to_order_by_taxonomy_name' );
Registering the taxonomy
add_action( 'init', 'jp_register_project_taxonomies', 0 );
function jp_register_project_taxonomies() {
// register taxonomy to hold our clients
register_taxonomy(
'client',
array( 'post' ),
array(
'hierarchical' => false,
'public' => true,
'query_var' => true,
'rewrite' => true,
'labels' => array(
'name' => _x( 'Clients', 'taxonomy general name' ),
'singular_name' => _x( 'Client', 'taxonomy singular name' ),
'search_items' => __( 'Search Clients' ),
'all_items' => __( 'All Clients' ),
'edit_item' => __( 'Edit Client' ),
'update_item' => __( 'Update Client' ),
'add_new_item' => __( 'Add New Client' ),
'new_item_name' => __( 'New Client Name' ),
'menu_name' => __( 'Client' )
),
)
);
}
pre_get_posts Query
add_action( 'pre_get_posts', 'jp_project_taxonomy_queries' );
function jp_project_taxonomy_queries( $query ) {
if(!is_admin() && $query->is_main_query() && is_category() ):
if (get_query_var('poby') == 'client'):
$taxonomies = array();
$tax_order = (get_query_var('po') == 'DESC' ? 'DESC' : 'ASC' );
foreach (get_terms('client', array('order' => $tax_order)) as $tax ) {
$taxonomies[] = $tax->name;
}
$taxquery = array(
array(
'taxonomy' => 'client',
'terms' => $taxonomies,
'field' => 'slug',
)
);
$query->set( 'tax_query', $taxquery );
$query->set( 'orderby', 'how_to_order_by_taxonomy_name' );
endif;
endif;
}
As far as I know, there is no parameter to order a WP_Query by term.
My solution to this problem usually is to create a meta field that contains the term name or the term slug. This field is created when the post is saved using the hook save_post or save_post_{post_type}.
For instance, to order a list of books (post type book) by year of publication (taxonomy date) we can use this function to create the meta field:
add_action( 'save_post_book', 'prefix_save_date_as_meta', 10 );
function prefix_save_date_as_meta ($post_id) {
$years = get_the_terms($post_id,'date');
if( empty($years) )
return;
$years_list = wp_list_pluck($years,'name');
update_post_meta($post_id,'_book_date',$years_list[0]);
return;
}
And this other function to order the query:
add_filter( 'pre_get_posts', 'prefix_custom_args_for_loops' );
function prefix_custom_args_for_loops( $query ) {
if ( !is_admin() && is_post_type_archive('book') && $query->is_main_query() ) {
$query->set( 'orderby','meta_value_num');
$query->set( 'meta_key','_book_date');
}
return $query;
}

How to add custom field to custom post type?

Good morning.
I have created a custom post type called 'Products'. I want to create a custom field (is metabox
the correct term?) where my client can tick a box to determine whether a given post within this
CPT is a featured post.
Here is the code in my functions.php to create the 'Products' CPT:
function products_custom_init() {
$labels = array(
'name' => _x('Products', 'post type general name'),
'singular_name' => _x('Product', 'post type singular name'),
'add_new' => _x('Add New', 'products'),
'add_new_item' => __('Add New Product'),
'edit_item' => __('Edit Product'),
'new_item' => __('New Product'),
'view_item' => __('View Product'),
'search_items' => __('Search Products'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'query_var' => true,
'rewrite' => array('slug','pages'),
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail','excerpt',)
);
register_post_type( 'products' , $args );
}
add_action( 'init', 'products_custom_init' );
So how do I add the 'featured' metabox / custom field to only Products posts?
Many thanks,
Cynthia
If you want to create a custom meta box inside a custom post type you will need to use 3 functions.
A function to create a custom "meta boxes" block/screen on your post edit screen: add_meta_boxes_{$post_type}
A function to add a input field to change/show your custom meta
And finally a function to save your meta along with the rest of the post when you click save on the post edit screen: save_post_{$post->post_type}
In your case, a custom checkbox would look like this:
add_action( 'add_meta_boxes_products', 'meta_box_for_products' );
function meta_box_for_products( $post ){
add_meta_box( 'my_meta_box_custom_id', __( 'Additional info', 'textdomain' ), 'my_custom_meta_box_html_output', 'products', 'normal', 'low' );
}
function my_custom_meta_box_html_output( $post ) {
wp_nonce_field( basename( __FILE__ ), 'my_custom_meta_box_nonce' ); //used later for security
echo '<p><input type="checkbox" name="is_this_featured" value="checked" '.get_post_meta($post->ID, 'team_member_title', true).'/><label for="is_this_featured">'.__('Featured Product?', 'textdomain').'</label></p>';
}
add_action( 'save_post_team_member', 'team_member_save_meta_boxes_data', 10, 2 );
function team_member_save_meta_boxes_data( $post_id ){
// check for nonce to top xss
if ( !isset( $_POST['my_custom_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['my_custom_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// check for correct user capabilities - stop internal xss from customers
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// update fields
if ( isset( $_REQUEST['is_this_featured'] ) ) {
update_post_meta( $post_id, 'is_this_featured', sanitize_text_field( $_POST['is_this_featured'] ) );
}
}
As Muhammad Yasin said there are plugins I'd recommend:
http://wordpress.org/extend/plugins/more-fields/
if you want to do it yourself in code look at: add_meta_box
<?php add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); ?>
You can register boxes per post type.
You can use this plugin
http://wordpress.org/extend/plugins/types/
or this tutorial may helpful for you.
http://wptheming.com/2010/08/custom-metabox-for-post-type/

Trying to get both custom post type and posts to show up in tag and category pages

I've built a custom post type, that is set to use the out-of-the-box tags and categories that posts use. However, if I click on a tag or category link, the archive only shows posts with that tag, not my custom post type. I've tried a few ways to fix it but they don't seem to be working. My code is:
// Add Resource Post Type
add_action('init', 'hallam_init');
function hallam_init() {
// set up the labels
$labels = array(
'name' => _x('Resources', 'post type general name'),
'singular_name' => _x('Resource', 'post type singular name'),
'add_new' => _x('Add New', 'resource'),
'add_new_item' => __( 'Add New Resource' ),
'edit_item' => __( 'Edit Resource' ),
'new_item' => __( 'New Resource' ),
'view_item' => __( 'View Resource' ),
'search_items' => __( 'Search Resources' ),
'not_found' => __( 'No resources found' ),
'not_found_in_trash' => __( 'No respources found in Trash' ),
'parent_item_colon' => ''
);
// set up the args
$args = array (
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array (
'slug' => 'resources'
),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments'
),
'taxonomies' => array(
'collection',
'category',
'post_tag'
),
'has_archive' => true
);
register_post_type('ht_resource', $args);
}
// Add Taxonomy
register_taxonomy('collection', 'ht_resource', array(
'hierarchical' => true,
'label' => 'Collections',
'query_var' => true,
'rewrite' => true
));
// Fix the archives
add_filter( 'pre_get_posts', 'add_to_query' );
function add_to_query( $query ) {
// if ( is_home() ) {
if( $query->query_vars['suppress_filters'] ) // TODO check if necessary
return $query;
$supported = $query->get( 'post_type' );
if ( !$supported || $supported == 'post' )
$supported = array( 'post', 'ht_resource' );
elseif ( is_array( $supported ) )
array_push( $supported, 'ht_resource' );
$query->set( 'post_type', $supported );
return $query;
//}
}
Am I missing something obvious?
Can you plz try this by adding on your theme's functions.php? Hope it will work
function query_post_type($query) {
if(is_tag()) {
$query->set('post_type',$post_types=get_post_types('','names'));
return $query;
}
}
add_filter('pre_get_posts', 'query_post_type');
thnx

Resources