get_term and get_term_by return null or false, even though term exists

To use get_term_by we need the term object before it gets deleted. Therefore, we should use the action hook delete_term_taxonomy that runs before the term is deleted. Now, we can go ahead and perform some tasks such as deleting all the postmeta related to the term being deleted. The code is: add_action( ‘delete_term_taxonomy’, function($tt_id) { … Read more

Hide posts having children terms when display posts by category in edit.php

By default when you run a query for a hierarchical taxonomy term, WordPress also returns post from its children. This happen on backend and on frontend as well. ‘tax_query’ argument has an argument ‘include_children’ that was introduced for the scope, from Codex: include_children (boolean) Whether or not to include children for hierarchical taxonomies. Defaults to … Read more

Enable the admin to sort custom taxonomy

Thanks to this great tutorial, I just found the answer myself. WordPress offers the action hooks sb_reference_category_add_form_fields, sb_reference_category_edit_form_fields, create_sb_reference_category and edited_sb_reference_category (these are named after the custom taxonomy, in my case sb_reference_category). With these hooks, it is easy as pie to add and save a custom field to the taxonomy.

Custom Taxonomy Template Error: Catchable fatal error: Object of class WP_Error could not be converted to string

Well, you echo an object. Never just echo stuff if you are not sure what you get in return. Take a look at the function: The error itself is quite clear: function get_term_link( $term, $taxonomy = ”) { global $wp_rewrite; if ( !is_object($term) ) { if ( is_int($term) ) { $term = get_term($term, $taxonomy); } … Read more

Get posts from taxonomy URL

So I’m taking some liberties with this, I’m assuming a couple things: You know the taxonomy the term belongs to You have the term archive link So if I have the term archive link: $url=”http://www.example.com/taxonomy/term/”; I know that the last part of that url is going to be the terms slug. I can use the … Read more

Get terms cross-taxonomy query

All right, few hours of figuring it out, but I made it: I had to wrap the whole thing into a wp_query, querying the current ‘category’ term and then use get_the_terms instead of get_terms to get the terms for the current post query, then remove duplicates and echo it in a foreach loop similar to … Read more

Same taxonomy for different object types

Those are different object types that belong to different tables and hence can have same primary key ID. –> Even though these are different types, all are being stored in one table i.e. wp_posts with post_type = the different types. Check the table structure for clarity. So there won’t be any same ID conflict. if … Read more