php - Filter post's category options with hook
Get the solution ↓↓↓On the post edit page I have a category selection box. I would like to filter the displayed categories. How can I do this in PHP? I've tried finding the source of this box in the wordpress-src directory, but haven't had any luck. Can I hook into this and filter what's displayed?
Answer
Solution:
I found a similar question answered here on the WordPress Stack Exchange: https://wordpress.stackexchange.com/questions/99034/how-i-can-hide-some-category-from-author-admin-panel
/*
* Hide Specified Categories (by ID) from authors
*/
add_action( 'admin_init', 'wpse_55202_do_terms_exclusion' );
function wpse_55202_do_terms_exclusion() {
//Where is the string 'author' you need to replace it with your users' role
if( current_user_can('author') )
add_filter( 'list_terms_exclusions', 'wpse_55202_list_terms_exclusions', 10, 2 );
}
function wpse_55202_list_terms_exclusions($exclusions,$args) {
// edit 3 with your category's ID
return $exclusions . " AND ( t.term_id <> 3 ) ";
}
Tweak the answer from there to filter out any tag IDs you want hidden, you can also filter for specific user roles or remove that piece.
Answer
Solution:
There are plugins that you can use which already achieve this type of functionality.
See Taxonomy Filter
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.