Adding new terms to custom taxonomy

after_setup_theme fires before init, so in this case the code is trying to insert a term into your taxonomy before WordPress is aware that your taxonomy exists! Generally, I feel like init is a better location for both pieces of functionality unless you have a reason to do otherwise. But however you resolve it, if … Read more

wp_get_object_terms count on taxonomies within an category archive

A simple function to count posts did the trick, but this is one more request in DB : function count_posts_in_cat_by_custom_term( $category_ID, $custom_taxonomy_name, $custom_term_id ){ $args = array( ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘category’ => $category_ID, ‘tax_query’ => [ [ ‘taxonomy’ => $custom_taxonomy_name, ‘fields’=>’term_id’, ‘terms’ => $custom_term_id, ] ] ); $posts = … Read more

get_the_terms inside save_post gives old terms

This issue shouldn’t happen if you’re using the classic editor, however, if you’re using the block/Gutenberg editor which uses the REST API, then that issue can be fixed by using the wp_after_insert_post hook instead. Excerpt from https://make.wordpress.org/core/2020/11/20/new-action-wp_after_insert_post-in-wordpress-5-6/: The new action wp_after_insert_post has been added to WordPress 5.6 to allow theme and plugin developers to run … Read more

Code to show related posts (custom post type and custom taxonomy/category) is messing with other code

It seemed like you did not call wp_reset_postdata() which in short, restores the context of the template tags (e.g. the_title() and get_the_title()) from a secondary query loop back to the main query loop. More details at https://developer.wordpress.org/reference/functions/wp_reset_postdata/#more-information. So basically, you just need to call wp_reset_postdata() like so, and then the issue in question would disappear: … Read more

Get X posts with the same terms as the current post (custom post type and custom taxonomy)

It’s not limiting to 5 posts That’s because the posts_per_page arg should actually be in the same level as the post_type arg and not put inside the tax_query array. I.e. $args = array( ‘post_type’ => ‘oferta’, ‘posts_per_page’ => 5, // add it here ‘tax_query’ => array( array( ‘taxonomy’ => ‘categoria’, ‘posts_per_page’ => 5, // NOT … Read more