I'm creating different custom post types and taxonomies and I want to remove the 'Post Tags' taxonomy from the default 'Posts' post type. How do I go about doing this?
Thanks.
I suggest you don't mess with the actual global. Its safer to simply deregister the taxonomy from the post type: register_taxonomy is used for both creation and modification.
function ev_unregister_taxonomy(){
register_taxonomy('post_tag', array());
}
add_action('init', 'ev_unregister_taxonomy');
To remove the sidebar menu entry:
// Remove menu
function remove_menus(){
remove_menu_page('edit-tags.php?taxonomy=post_tag'); // Post tags
}
add_action( 'admin_menu', 'remove_menus' );
Perhaps a more technically correct method would be to use unregister_taxonomy_for_object_type
add_action( 'init', 'unregister_tags' );
function unregister_tags() {
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
Where it says 'taxonomy_to_remove' is where you'll enter the taxonomy you want to remove. For instance you can replace it with the existing, post_tag or category.
add_action( 'init', 'unregister_taxonomy');
function unregister_taxonomy(){
global $wp_taxonomies;
$taxonomy = 'taxonomy_to_remove';
if ( taxonomy_exists( $taxonomy))
unset( $wp_taxonomies[$taxonomy]);
}
Total unregister and remove (minimal PHP version 5.4!)
add_action('init', function(){
global $wp_taxonomies;
unregister_taxonomy_for_object_type( 'category', 'post' );
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
if ( taxonomy_exists( 'category'))
unset( $wp_taxonomies['category']);
if ( taxonomy_exists( 'post_tag'))
unset( $wp_taxonomies['post_tag']);
unregister_taxonomy('category');
unregister_taxonomy('post_tag');
});
There is new function to remove taxonomy from WordPress.
Use unregister_taxonomy( string $taxonomy ) function
See details: https://developer.wordpress.org/reference/functions/unregister_taxonomy/
Use it in 'admin_init' hook insetead not 'init'
function unregister_taxonomy(){
register_taxonomy('post_tag', array());
}
add_action('admin_init', 'unregister_taxonomy');
add_action('admin_menu', 'remove_menu_items');
function remove_menu_items() {
remove_submenu_page('edit.php','edit-tags.php?taxonomy=post_tag');
}
Related
I have the CPT (Custom Post Type) "news".
In the "category" taxonomy I have "food" and "health".
The archive page is accessed via the url:
www.mysite.com/news
Is there any way to show "news" by categories? Ex:
www.mysite.com/food/news
www.mysite.com/health/news
Thank you all in advance.
In that link I found the following solution:
The solution for me had three parts. In my case the post type is called trainings.
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the
register_post_type function.
Change the slug to have a dynamic
category. "Listen" to the new dynamic URL and load the appropriate template.
So here is how to change the permalink dynamically for a given post type. Add to functions.php:
function vx_soon_training_post_link( $post_link, $id = 0 ) {
$post = get_post( $id );
if ( is_object( $post ) ) {
$terms = wp_get_object_terms( $post->ID, 'training_cat' );
if ( $terms ) {
return str_replace( '%cat%', $terms[0]->slug, $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'vx_soon_training_post_link', 1, 3 );
...and this is how to load the appropriate template on the new dynamic URL. Add to functions.php:
function archive_rewrite_rules() {
add_rewrite_rule(
'^training/(.*)/(.*)/?$',
'index.php?post_type=trainings&name=$matches[2]',
'top'
);
//flush_rewrite_rules(); // use only once
}
add_action( 'init', 'archive_rewrite_rules' );
Thats it! Remember to refresh the permalinks by saving the permalinks again in de backend. Or use the flush_rewrite_rules() function.
But I have a question that I couldn't do there:
This part:
Add 'rewrite' => array('slug' => 'trainings/%cat%') to the register_post_type function.
where do I do that? Or is this already embedded in the later code?
I am fluent in HTML and CSS but not so much in PHP. Any help would be appreciated.
I have a custom function in Wordpress as seen below:
function prefix_calculation_global_calculation_vars( $vars ) {
return array(
'setup_cost' => 200,
);
}
add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' );
Is it possible to call an Advanced Custom Fields (ACF) field or a Pods field and have the value inserted into the function above where the 200 is?
Providing get_the_ID() works you could try this:
function prefix_calculation_global_calculation_vars( $vars ) {
return array(
'setup_cost' => get_field('setup_cost', get_the_ID()),
);
}
add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' );
You would have to grab it via the $wpdb->postmeta table
<?php echo get_post_meta(get_the_ID(), 'setup_cost', TRUE); ?>
I found this 2 filters for WooCommerce to extend the membership plan list:
add_filter( 'manage_edit-wc_user_membership_columns', array( $this, 'customize_columns' ) );
add_filter( 'manage_edit-wc_user_membership_sortable_columns', array( $this, 'customize_sortable_columns' ) );
I want to add a new column with the memberships plan id to show.
any suggestion on how to use that in the functions.php
You found the correct filter manage_edit-wc_user_membership_columns – it allows to add a column in membership plans, example:
add_filter( 'manage_edit-wc_user_membership_columns', 'my_add' );
function my_add( $columns ) {
$columns['id_of_the_plan'] = 'Memberships plan id';
return $columns;
}
Once you insert this code in your current theme functions.php file or in a custom plugin, the column will appear. Now it is time to add the data to it. manage_posts_custom_column will help with it.
add_action( 'manage_posts_custom_column', 'my_id' );
function my_id( $column ) {
if( $column == 'id_of_the_plan' ) {
$m = wc_memberships_get_user_membership( get_the_ID() );
echo $m->plan_id;
}
}
The original code is taken from this example.
I have a custom function that works with my custom post type. While porocessing save_post action:
add_action( 'save_post', 'my_custom_function' );
I would like to set post status as draft (in case of a problem with getting custom data from outside api).
In my my_custom_function function I have this little block:
if ($error == true) {
$override_post = array();
$override_post['ID'] = $post_id;
$override_post['post_status'] = 'draft';
wp_update_post( $override_post );
}
but it seems, that after save_post is being processed, then post_status is being set again.
Anybody have an idea, where should I hook into, so while saving post data I can modify its post_status, post_date and some other post data informations so they are not being overriten?
You should hook it to wp_insert_post_data. Then you could use a function like this to set your post status to draft:
add_filter( 'wp_insert_post_data', 'set_post_to_draft', 99, 2 );
function set_post_to_draft( $data, $postarr ) {
if ( your_condition ) {
$data['post_status'] = 'draft';
}
return $data;
}
I had to make a post type with only one post_status option, and it seem to fit your needs too, as it is works exactly with the save_post hook.
add_action( 'save_post', 'my_function' );
function my_function( $post_id ){
if ( ! wp_is_post_revision( $post_id ) ){
// avoid endless circle
remove_action('save_post', 'my_function');
// update the data before saving
wp_update_post( wp_slash([
'ID' => $_POST['ID'],
'post_status' => 'draft'
]));
// restore the saving hook
add_action('save_post', 'my_function');
}
}
The original solution found here:
https://wp-kama.ru/function/wp_update_post
I am trying to get my permalink to be something like this:
http://example.com/projects/1234
By default it is looking like this:
http://example.com/projects/title
I tried setting the "slug" to "projects/%post_id%" but then it looked like this:
http://example.com/projects/%post_id%/title
Is there a way to set the slug to my custom slug "/" the id for the post? Any help is appreciated.
A gift from me to you: Just replace "property" with the name of your custom post type. Goes in your theme's functions.php
add_filter('post_type_link', 'property_post_type_link', 1, 3);
function property_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'property' ){
return home_url( 'property/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'property_rewrites_init' );
function property_rewrites_init(){
add_rewrite_rule(
'property/([0-9]+)?$',
'index.php?post_type=property&p=$matches[1]',
'top' );
}
Is 'projects' a category or tag etc?
Something like the below should work if projects is a category or tag.
/%category%/%post_id%/