How do I remove a taxonomy from one post? - wordpress

With
wp_set_object_terms($postId, $termsList, $taxonomy, true );
I set taxonomy on my posts (products for woocommerce, from an external feed).
How I can remove the taxonomy for one post, when termsList is empty?

if( empty( $termsList) ){
wp_remove_object_terms( $postId, 'term-to-remove', $taxonomy );
}
https://developer.wordpress.org/reference/functions/wp_remove_object_terms/

Related

How to block certain tags from creating in WordPress

Is there any WordPress theme function code that blocks certain wordpress tags from being created? I'd want to exclude some tags from the keyword list, for example, I don't want WordPress to create the following stop-words tags:
adult, bikini, enjoyment, fun, block, admin
You can use the pre_insert_term filter hook. that will help you to prevent tags before inserting. try the below code.
function prevent_some_tags_from_being_add( $term, $taxonomy ){
if( $taxonomy == 'post_tag' ){
$prevent_tags = array( 'adult', 'bikini', 'enjoyment', 'fun', 'block', 'admin' );
if( in_array( $term, $prevent_tags ) ){
return new WP_Error( 'invalid_term', __( 'Sorry this tag is not allowed.' ) );
}
}
return $term;
}
add_filter( 'pre_insert_term', 'prevent_some_tags_from_being_add', 10, 2 );

Automatically set custom taxonomy to custom post type

I have a Custom Post Type property and Custom Taxonomies as city and state. This is not hierarchical taxonomies, but they already has relationships (each city marked with it's state). I want to set state automatically that depends of the chosen city. What action or function can help?
I tried to use add_action( 'save_post', 'save_property_meta' ); with wp_set_object_terms, but I don't know how to write a condition.
add_action( 'save_post', 'save_property_meta' );
function save_property_meta( $post_id ) {
$terms = get_the_terms( $post->ID, 'property_city' );
if( $terms ){
$term = array_shift( $terms );
$term->id;
}
/** It's should be condition**/
wp_set_object_terms( get_the_ID(), get_term_meta( $city_term['id'], 'property_city_county'), true);
}

Why my custom posts not showing under categories

I have a custom post type called reviews. I have added a category taxonomy and had added my custom post types under the new category taxonomy. When I click on that category it doesn't show any of my custom posts. It will only show regular posts.
Any suggestions on how I can fix that?
use the below function and put this on your themes functions.php
function add_reviews_post_type( $query ) {
if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'post_type', array(
'post', 'reviews'
));
}
return $query;
}
add_filter( 'pre_get_posts', 'add_reviews_post_type' );

Rewrite rules for EDD product page

I want my url changes depending on the term of taxonomy category_download custom post types download.
Example:
I have 2 categories:
– plugins
– themes
I wish I had a url for my single download:
/plugins/name_of_download/
AND
/themes/name_of_download/
Try adding this code to your themes functions.php file,
function __custom_messagetypes_link( $link, $term, $taxonomy )
{
if ( $taxonomy == 'plugins' ){
return str_replace( 'plugins/', '', $link );
}
else if ( $taxonomy == 'themes' ){
return str_replace( 'themes/', '', $link );
}
else{
return $link;
}
}
add_filter( 'term_link', '__custom_messagetypes_link', 10, 3 );
I am assuming the used slug, May be you have to change the your taxonomy slug if these are different.
If have any problem you can freely ask, Thanks,

Set Catalog visibility hidden woo-commerce

How to set Catalog visibility hidden in woo-commerce WordPress programmatically?
Like its mentioned here :
https://docs.woothemes.com/document/catalog-visibility-options/
But i can't find any hook or hack, that how to do it in PHP.
I have tried doing this for some days, and there is nothing about it online so I read the woocommerce documentation and discovered that in woocommerce 3.x.x the visibility is a taxonomy called "product_visibility".
To achieve that you should set taxonomy terms, for example:
//Set product hidden:
$terms = array( 'exclude-from-catalog', 'exclude-from-search' );
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
//Set product visible in catalog:
$terms = 'exclude-from-search';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
//Set product visible in search:
$terms = 'exclude-from-catalog';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
All possible taxonomy terms:
"exclude-from-catalog"
"exclude-from-search"
"featured"
"outofstock"
The visibility is set in the custom field _visibility. You can change it with update_post_meta():
update_post_meta( $product_id, '_visibility', '_visibility_hidden' );
Possible values:
visible (Catalog & Search)
catalog (Catalog only)
search (Search only)
hidden (nowhere)

Resources