Wordpress: Hide categories from add new post page - wordpress

I would like to forbid the contributors from adding categories to posts. What I found out was this function remove_post_type_support(). However in the documentation it doesn't talk about tags or categories. I tried remove_post_type_support( 'post', 'category' ); and remove_post_type_support( 'post', 'categories' ); but neither worked.
Is there a way of doing so?

Try this, it is untested but I think it would work.
Simply deregister the taxonomy from the post type: register_taxonomy is used for both creation and modification.
Put it in your function.php file
if ( current_user_can('contributor') )
function contributors_unregister_categories() {
register_taxonomy( 'category', array() );
}
add_action( 'init', 'contributors_unregister_categories' );
}

Related

In Wordpress, how to add taxonomy to theme created post type?

The theme I'm using includes a custom post type. I need to add categories and tags to this post type. To avoid editing the theme or creating a child theme I created a simple plugin that I thought would add the taxonomy:
add_action( 'plugins_loaded', 'gp_register_taxonomy_for_object_type' );
function gp_register_taxonomy_for_object_type() {
register_taxonomy_for_object_type( 'post_tag', 'service' );
register_taxonomy_for_object_type( 'category', 'service' );
};
I tried init instead of plugins_loaded but that didn't work either.
Turns out admin_init was the golden ticket here:
add_action( 'admin_init', 'gp_register_taxonomy_for_object_type' );
function gp_register_taxonomy_for_object_type() {
register_taxonomy_for_object_type( 'post_tag', 'service' );
register_taxonomy_for_object_type( 'category', 'service' );
};
You’ll see that the plugins_loaded action is triggered before the theme is loaded. The first action available for themes to hook into is the after_setup_theme action.
Have a look at the wp-settings.php file.
Add this to your function.php
add_action( 'admin_init', 'register_taxonomy_for_new_post_type' );
function register_taxonomy_for_new_post_type() {
register_taxonomy_for_new_post_type( 'post_tag', 'post_type_name' );
register_taxonomy_for_new_post_type( 'category', 'post_type_name' );
}

shortcode for displaying custom taxonomy with description in pods plugin - wordpress

i have created a custom taxonomy named 'singer' using pods plugin and inside that plugin i have defined a label named 'details'. what i want to do is to generate a short code in which returns that details. i have searched my way through lots of documentation, but could not find one.image showing the custom field i have added inside the taxonomy
Thanks for helping!
Do you need it to be that complicated? (Meaning, do you need a plugin?)
Register a new taxonomy in your functions "singer":
function taxonomies_init() {
// create a new taxonomy
register_taxonomy(
'singer',
'post',
array(
'label' => __( 'Singer' ),
'rewrite' => array( 'slug' => 'singer' ),
)
);
}
add_action( 'init', 'taxonomies_init' );
Register the shortcode:
function showtax_func( $atts ) {
if (is_single()) {
$a = shortcode_atts( array(
'tax' => '',
), $atts );
$termname = get_the_terms(get_the_ID(),$a['tax'])[0]->name;
return $termname;
}
}
add_shortcode( 'show_tax', 'showtax_func' );
Use the shortcode like this: [show_tax tax="singer"]
You can add more taxonomies by expanding the first function by duplicating the register_taxonomy() function. And get any taxonomy with the shortcode by just changing the value of the taxonomy name.

Wordpress: Remove 'uncategorized' upon theme activation

Is there a way to remove the default 'uncategorized' category (from categories in Wordpress admin) upon activating my theme?
This will have to go in functions.php obviously.
As long as you have at least one other category to set as the default, you should be able to go to Settings>>Writing and change the default post category to one of your other categories. After doing this you should be able to delete Uncategorized by going to Posts>>Categories and deleting it there.
You can also change the Uncategorized default category by adding this to functions.php:
// Uncategorized ID is always 1
wp_update_term(1, 'category', array(
'name' => 'hello',
'slug' => 'hello',
'description' => 'hi'
));
as shown in this post: https://wordpress.stackexchange.com/questions/83415/remove-rename-uncategorized-category-in-wordpress
More info on wp_update_term
You can use the wp_delete_category function
<?php wp_delete_category( $cat_ID ) ?>
But you need at least 1 category in WordPress, so as long as you're create extra ones before hand you should be ok.
wp_delete_category reference page:
http://codex.wordpress.org/Function_Reference/wp_delete_category
This Works for me
add_filter( 'woocommerce_product_categories_widget_args', 'remove_uncategorized_category' );
add_filter( 'woocommerce_product_subcategories_args', 'remove_uncategorized_category' );
function remove_uncategorized_category( $args ) {
$uncategorized = get_option( 'default_product_cat' );
$args['exclude'] = $uncategorized;
return $args;
}

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