List only first-level children of specific custom taxonomy term

First of all you need the term id of hydro term, you can retrive it using get_term_by $hydro = get_term_by(‘slug’, ‘hydro’, ‘product_cat’); After that, you can use the term id as ‘parent’ argument for get_terms $hydro_children = get_terms( ‘product_cat’, array( ‘parent’ => $hydro->term_id ) ); Now you can display a list of this children: if … Read more

Automatically assign taxonomy term if custom meta value exists

You should hook onto the save_post action. add_action( ‘save_post’, ‘add_video_taxonomy’ ); function add_video_taxonomy( $post_id ) { // you should check if the current user can do this, probably against publish_posts // you should also have a nonce check here as well if( get_post_meta( $post_id, ‘video_url’, true ) ) { wp_set_post_terms( $post_id, ‘video’, ‘your_custom_taxonomy_name’, true ); … Read more

How do I get taxonomy terms by ID in a specific order

get_terms does not support a post__in argument and I don’t see any other argument that will let you force an order. You can, however, accomplish this with one fof at least two filters: function gto_forced_order($orderby) { $orderby = “FIELD(t.term_id,5,1,4)”; return $orderby; } add_filter(‘get_terms_orderby’,’gto_forced_order’); $terms = get_terms(‘category’); var_dump($terms); Or… function gto_forced_order($pieces) { $pieces[‘orderby’] = “ORDER BY … Read more

Get multiple term objects by ids

I wonder if you mean something like this modified Codex example: // Fetch: $terms = get_terms( ‘category’, array( ‘include’ => array( 1, 2, 3 ), ) ); // Output: if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $li = ”; foreach ( $terms as $term ) { $li .= sprintf( … Read more

What are terms and taxonomy, how they related to post and how these three are stored in database?

What are the terms and taxonomy in wordpress? From WordPress.org’s Taxonomy Codex… In WordPress, a “taxonomy” is a grouping mechanism for some posts (or links or custom post types)… The names for the different groupings in a taxonomy are called terms. Using groupings of animals as an example, we might call one group “birds”, and … Read more

Get post terms for multiple posts at once?

That would be wp_get_object_terms. You’d need an array of post id’s as the input, together with the taxonomy you want to retrieve the terms for (let’s say: books). This should work (untested): $my_query = new wp_query ($args); $my_posts = $my_query->posts; $my_post_ids = wp_list_pluck ($my_posts, ‘ID’); $my_terms = wp_get_object_terms ($my_post_ids, ‘books’);

Get a list of Terms for a specific category

The following code will do it. Please change the ‘filter’ text in the below code to whatever filters taxonomy name you have set. if(is_category() ){ $thiscat = get_queried_object_id(); $filter_tax = array(); $args = array( ‘category’ => $thiscat ); $lastposts = get_posts( $args ); foreach ( $lastposts as $post ){ setup_postdata( $post ); $terms = get_the_terms( … Read more

get_terms parent for current product only

get_terms relates to getting all the terms of a taxonomy. get_the_terms grabs all the terms related to the post. The problem is that it sounds like you only want to return those terms which are parent categories, not the children, and get_the_terms does not pass an arguments array. $terms = get_the_terms( get_the_ID(), ‘product_cat’ ); foreach … Read more