Fixing category count

If you just want to update the counts of posts in each term, wp_update_term_count_now( $terms, $taxonomy ) should do it… just pass the terms affected as an array and run it once for each taxonomy you have. You can also call wp_defer_term_counting( true ) before inserting new rows, and then after adding your posts, catch … Read more

get_term_by not working when in functions.php

This is probably happening because the taxonomy you’re trying to query is registered yet. Eg. The WordPress environment is loaded when a theme’s functions.php file loads, but many plugins/themes/core functions don’t register taxonomies until later. Try hooking into init with a really high priority number and running the get_term_by function. Like so: <?php add_action( ‘init’, … Read more

Order terms by term_order

As this is one of the top results on Google and none of the above quite worked for me, this did seem to return terms in an order matching their display in the admin… get_terms([ ‘taxonomy’ => ‘whatever_you_want’, ‘meta_key’ => ‘order’, ‘orderby’ => ‘meta_value_num’ ]); In case it helps anyone, WP stores a value in … Read more

get term by id without taxonomy

Yes, but you need you make your own SQL query. A modified version of WP’s get_term(): function &get_term_by_id_only($term, $output = OBJECT, $filter=”raw”) { global $wpdb; $null = null; if ( empty($term) ) { $error = new WP_Error(‘invalid_term’, __(‘Empty Term’)); return $error; } if ( is_object($term) && empty($term->filter) ) { wp_cache_add($term->term_id, $term, ‘my_custom_queries’); $_term = $term; … Read more

Passing a hardcoded page/post ID into `get_post`

For starters let’s dive into what is 5 really. It is the post’s ID. But what is ID in turn? It is value in the MySQL table row which identifies the specific post record. Issues with using IDs So first there are some conceptual problems with it. It’s not content. It’s not something user creates, … Read more

Hide the term description on the term edit page, for a given taxonomy

You could target the edit form for the post_tag taxonomy, through the post_tag_edit_form hook: /** * Hide the term description in the post_tag edit form */ add_action( “post_tag_edit_form”, function( $tag, $taxonomy ) { ?><style>.term-description-wrap{display:none;}</style><?php }, 10, 2 ); Here you can also target an individual tag. If you need something similar for other taxonomies, you … Read more

Control term order on a per-post basis

I’m not sure if I understand exactly what your trying to accomplish but I’ve outlined a way for you to sort the order of terms associated with the current post. Html for the term order metabox: echo ‘<ul id=”the-terms”>’ $terms = get_the_terms( $post->ID, $taxonomy ); foreach ( $terms as $term ) { echo ‘<li class=”item” … Read more