I'm trying to update custom taxonimies "brands" created with jetengine plugin through rest api remotely and i've added the following snippet:
// Register the 'brands' field for the 'product' post type in the REST API
add_action('rest_api_init', function() {
? ? register_rest_field('product', 'brands', array(
? ? ? ? 'get_callback' => function($product) {
? ? ? ? ? ? return wp_get_post_terms($product['id'], 'brands');
? ? ? ? },
? ? ? ? 'update_callback' => function($meta, $product) {
? ? ? ? ? ? if (!empty($meta)) {
? ? ? ? ? ? ? ? $term_ids = array();
? ? ? ? ? ? ? ? foreach ($meta as $term) {
? ? ? ? ? ? ? ? ? ? if (isset($term['term_id']) && is_numeric($term['term_id'])) {
? ? ? ? ? ? ? ? ? ? ? ? $term_ids[] = (int) $term['term_id'];
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (!empty($term_ids)) {
? ? ? ? ? ? ? ? ? ? wp_set_post_terms($product->ID, $term_ids, 'brands');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return true; // Return true to indicate the update was successful
? ? ? ? },
? ? ? ? 'schema' => null,
? ? ));
});
// Handle PUT request to update the 'brands' taxonomy via API
add_action('rest_api_init', function() {
? ? register_rest_route('my_custom_namespace/v1', '/product/(?P<id>\d+)/brands', array(
? ? ? ? 'methods' => 'PUT',
? ? ? ? 'callback' => function($request) {
? ? ? ? ? ? $product_id = (int) $request['id'];
? ? ? ? ? ? $terms = $request->get_param('brands');
? ? ? ? ? ? if (!empty($terms) && is_array($terms)) {
? ? ? ? ? ? ? ? $term_ids = array();
? ? ? ? ? ? ? ? foreach ($terms as $term) {
? ? ? ? ? ? ? ? ? ? if (is_numeric($term)) {
? ? ? ? ? ? ? ? ? ? ? ? $term_ids[] = (int) $term;
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? $term_obj = get_term_by('name', $term, 'brands');
? ? ? ? ? ? ? ? ? ? ? ? if ($term_obj) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? $term_ids[] = $term_obj->term_id;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (!empty($term_ids)) {
? ? ? ? ? ? ? ? ? ? wp_set_post_terms($product_id, $term_ids, 'brands');
? ? ? ? ? ? ? ? ? ? return new WP_REST_Response('Brands updated successfully', 200);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return new WP_Error('no_terms', 'No valid terms provided', array('status' => 400));
? ? ? ? },
? ? ? ? 'permission_callback' => function() {
? ? ? ? ? ? return current_user_can('edit_posts');
? ? ? ? },
? ? ));
});
// Remote taxonomy update using wp_remote_post
$response = wp_remote_post( $api_url, array(
? ? 'method' => 'PUT',
? ? 'headers' => array(
? ? ? ? 'Authorization' => 'Basic ' . base64_encode( $api_key . ':' . $api_secret ),
? ? ? ? 'Content-Type' => 'application/json',
? ? ),
? ? 'body' => json_encode( array(
? ? ? ? 'brands' => array( 'term1', 'term2' ) // Replace with your terms
? ? ) ),
) );
I can see the taxonimies but i cannot update them, can you help?
]]>I installed plugin, it works well with posts and taxonomies by wordpress, but it doesn’t work with CPT UI (custom posts and custom taxonomies I created). I get a message that says my taxonomy is invalid. What could I do to fix that?
Thanks for your help!
]]>I’m getting lots of these warnings in error.log:
PHP Warning: Undefined variable $rest_api_route in ../varnish-http-purge/varnish-http-purge.php on line 976, referer: ../wp-admin/edit.php?product_tag=test&post_type=product&vhp_flush_do=object&_wpnonce=xxx&paged=2
It seems that $rest_api_route
is defined only If json is NOT disabled (line 885) and when purging Custom Taxonomies it doesn’t check if ( isset( $rest_api_route ) )
, as done for JSON API Permalink for the post based on type on line 903.
Kind Regards
Dave
]]>Despite successfully registering these taxonomies, I’m unable to see the taxonomy values within the PostX plugin’s block editor. The taxonomies work correctly in the default WordPress editor, but the issue arises specifically within the PostX block editor.
I have already attempted the following steps to resolve the issue:
However, the problem persists. I believe that there might be a compatibility issue between the custom taxonomies and the PostX plugin’s block editor.
Could you please provide guidance on how to troubleshoot and resolve this issue? Is there a specific setting or configuration within the PostX plugin that I need to adjust to ensure proper display of taxonomy values?
Thank you for your time and assistance. I appreciate your support in helping me resolve this matter.
The code is:
// Add the custom post type registration function to the init hook
add_action('init', 'create_podkast_post_type');
// Register the custom post type
function create_podkast_post_type() {
register_post_type('podkast', array(
'labels' => array(
'name' => ('Podkast'), 'singular_name' => ('Podkastlar'),
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'categories', 'tags'),
'rewrite' => array('slug' => 'podkast'),
'menu_icon' => 'dashicons-microphone', // Change to the appropriate dashicon
'taxonomies' => array('podkast_category', 'podkast_tag'), // Add this line to associate taxonomies
));
}
// Register the custom taxonomy for categories
add_action('init', 'create_podkast_taxonomy');
function create_podkast_taxonomy() {
register_taxonomy(
'podkast_category', // Change this to your desired taxonomy name
'podkast', // Change this to your custom post type name
array(
'hierarchical' => true,
'label' => __('Podkast Kateqoriyalari'),
'rewrite' => array('slug' => 'podkast-kateqoriya'),
)
);
}
// Register the custom taxonomy for tags
add_action('init', 'create_podkast_tags_taxonomy');
function create_podkast_tags_taxonomy() {
register_taxonomy(
'podkast_tag', // Change this to your desired taxonomy name for tags
'podkast', // Change this to your custom post type name
array(
'hierarchical' => false,
'label' => __('Podkast Etiketl?ri'),
'rewrite' => array('slug' => 'podkast-etiket'),
)
);
}
]]>