$post
is undefined in your filter. You need to explicitely invoke the $post
global inside your filter function to have it available. You must remember that variables (even global variables) outside a function will not be available inside a function, that is how PHP works.
You actually do not need to use the $post
global, the post ID is passed by reference as second parameter to the the_title
filter.
You can use the following:
add_action( 'the_title', 'adddd', 10, 2 );
function adddd( $title, $post_id )
{
if( has_category( 30, $post_id ) ) {
$title="Prefix " . $title;
}
return $title;
}
If you need to target only the titles of posts in the main query/loop, you can wrap your contional in an extra in_the_loop()
condition
Related Posts:
- Filter categories using tags
- Make parent categories not selectable
- Add custom category name as data-filter to switch between these categories
- Can paginate_links() be customized for a specific category or tag?
- Filtering by Category doesnt paginate correctly on the backend
- Dropdown category filter
- Make parent category not selectable when it has child categories
- Replace category titles
- Hide posts of a certain category unless logged in
- What’s the Simplest Way to Override/Rewrite the %category% Permalink Structure Tag?
- How to Filter categories in the permalink structure
- How to display posts under a specific subcategory of a given category
- Taxonomy filter all children
- Filter Home Page Posts in Buddypress [closed]
- Categorising posts/pages into years published
- Inserting Category programmatically
- Enter a variable in the ‘category_name’ parameter
- Automatically set default password to all posts in a specific category
- get_the_categories filter returns an empty array
- category filter doesn’t work in WPML
- Query posts from category based on a filter most favorited
- Filter Categories in meta box for CPTs
- Show only first category name in RSS?
- How to organize custom categories for filtering and selecting
- Filter $cpt categories to omit certain category
- How to set a filter by category within a customised page
- Refine/Filter the Search Results by Category
- Admin comments and user restrictions
- WordPress Post # of # filtered by category slug
- Pagination for ajax category filter
- ajax for filtering posts by category in wordpress loops
- How to hide category name
- How to make the ‘delete’ button inactive on some categories?
- Overriding default calendar to show posts from a category
- How can I display tags as categories?
- Display all product tags associated with a specific product category
- Filter WooCommerce archive pages by an additional category
- Portfolio Filter Buttons Wont Work
- “Virtual category page” based on a custom field filter
- Custom tax_query filter not working for Woocommerce product categories
- How to hide some categories in dashboard
- Filter By Category Dropdown List With Custom Taxonomies
- How can I filter posts by tag on the fly
- Change Default Content when Creating a Post based on Previous Category Choice
- Maintaining a separate posts page for certain categories and don’t show those on home page
- Exclude posts without category from loop
- How to filter post from categories only if the posts appears in one of them, not the other?
- How to fix select filter on category
- wp_dropdown_categories() works correctly but the list is not filtered in admin for custom post type. What is the problem?
- Add $args to wp_list_categories
- How to hide a category or tag everywhere EXCEPT for use with get_posts
- Add filter for specific category only
- How to create category filter on a blog like on the site below?
- How to filter tags by category?
- Custom Taxonomy Isotope Grid – Filter Buttons Not Working
- How to exclude a category returned by get_categories from function.php?
- Can’t display posts by filtering categories using isotope.js
- Correct Hook/Filter to amend category choices on post edit page
- Restrict retrieved terms by category?
- Hiding by default posts in given category except for some cases
- Remove “Category:”
- ACF Gallery with Media Categories – how do display categories and sort
- Custom filters inside a specific category?
- Include category name in page template
- filter single_cat_title avoiding the breadcrumbs
- Right way for setting categories
- Custom built theme won’t filter categories
- category filter doesn’t work
- How to create a photo gallery that can be filtered by the image categories
- Remove “?category=” in the URL wordpress ajax filter work for category
- Replace category titles inside RSS feed
- How to create an automatic MultiColoumn MegaMenu with Categories WordPress
- Is There a Difference Between Taxonomies and Categories?
- List all subcategories from category
- How To Find Out WordPress Category Table in MYSQL?
- Add custom field to Category
- Get Category ID inside Category template
- Only one category per post
- WordPress Multisite – global categories
- Get the children of the parent category
- Get posts from sites in Multisite?
- WP REST API: filter by category 1 AND category 2
- How To Get Parent Category Slug of Current Post
- Display All Products by Category with WooCommerce
- What is the difference between a “tag” and a “category”?
- Is there an easy way to make a meta box have the tabs like the Categories meta box has?
- How to check if I’m on the last page of posts?
- Display posts from the same category using next/previous post link
- How would I get a taxonomy/category list inside a Gutenberg block?
- Can I set a default featured image for a category?
- Check if a post is in any child category of a parent category
- How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms
- Force category choice before creating new post?
- Gutenberg editor add a custom category as wrapper for custom blocks
- WooCommerce: List All Categories
- How to get category and archive title?
- Get category ID from category slug (not working)
- How do I remove “Uncategorized” from posts with more than 1 category?
- Get rid of WordPress category, tag and author archives?
- Individual rss feed entry length for categories?