Hot answers tagged

10

Thanks to Ivaylo for this code, which was based on Bainternet's answer. The first function (get_term_top_most_parent) accepts a term ID and taxonomy and returns the the term's top-level parent (or the term itself, if it's parentless); the second function (hey_top_parents) works in the loop, and, given a taxonomy, returns the top-level ancestors of a ...


10

I'm afraid this isn't possible natively (yet?). See this trac: http://core.trac.wordpress.org/ticket/18106 Similarly on the taxonomy admin page the post count reflects all post types. (I'm pretty sure there is a trac ticket for that too) http://core.trac.wordpress.org/ticket/14084 See also, this related post. New solution Having written the one below, ...


8

Here is a simple function that will get you the top most parent term of any given term: function get_term_top_most_parent( $term_id, $taxonomy ) { $parent = get_term_by( 'id', $term_id, $taxonomy ); while ( $parent->parent != 0 ){ $parent = get_term_by( 'id', $parent->parent, $taxonomy ); } return $parent; } Once you have ...


8

This should work for you: $taxonomyName = "age"; //This gets top layer terms only. This is done by setting parent to 0. $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false)); echo '<ul>'; foreach ($parent_terms as $pterm) { //Get the Child terms $terms = ...


7

As you dive into WordPress, you'll find that WordPress has a lot of wrapper functions. For instance, there's add_theme_page that's just a wrapper of add_submenu_page. That's certainly not the only example (add_submenu_page itself has a bunch of wrappers, in fact). If you look at the source for get_categories(), you'll see that it too is a wrapper for ...


6

That is what you want: $woocommerce->cart->total;


5

You will need to filter the query, which you can do with the terms_clauses hook. This is very similar to G-M's solution but does not require a prefix on your search string. function old_style_name_like_wpse_123298($clauses) { remove_filter('term_clauses','old_style_name_like_wpse_123298'); $pattern = '|(name LIKE )\'%(.+%)\'|'; $clauses['where'] = ...


5

Since 3.1.0, get_ancestors() is available. It returns an array of ancestors from lowest to highest in the hierarchy.


5

tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following. $uposts = get_posts(array( 'post_type' => 'product', 'numberposts' => -1, 'tax_query' => array(array( 'taxonomy' => $cat->taxonomy, 'field' => 'slug', 'terms' => ...


5

You could do something like this <?php $terms = get_the_terms( $post->ID, 'taxonomy_name' ); ?> <div class="myclass<?php foreach( $terms as $term ) echo ' ' . $term->slug; ?>"> <!-- content --> </div> http://codex.wordpress.org/Function_Reference/get_the_terms


5

Public API for the rescue category is a Taxonomy that's already built in. Therefore it's quite easy to get its term count with get_terms( $tax, array( /* args */ ) ); $terms = get_terms( 'category', array( 'fields' => 'count', ) ); This will do a pretty fast COUNT (*) query so you don't have to worry about performance. Alternate solution You ...


4

OK had a go at this myself, I didn't realise ACF was able to add fields to taxonomies which is really handy so I wanted to figure it out too. <?php $libargs=array( 'hide_empty' => 0, 'parent' => 0, 'taxonomy' => 'library_categories'); ...


4

I accepted One Trick Pony's answer because it got me on the right track and it does answer my question. However, in the end I am using this, it returns full term object with it's taxonomy fields. Though it's a bit hacky... /** * Get ther without khowing it's taxonomy. Not very nice, though. * * @uses type $wpdb * @uses get_term() * @param int|object ...


4

The idea of term groups was to have a term with multiple aliases. This feature doesn't seem to be fully backed and is therefore practically never used.


4

Here is the example of get_terms in the Codex: $terms = get_terms("my_taxonomy"); $count = count($terms); if ( $count > 0 ){ echo "<ul>"; foreach ( $terms as $term ) { echo "<li>" . $term->name . "</li>"; } echo "</ul>"; } Lets say there was an 'other' term in my_taxonomy. To place it at the end ...


4

Ok, so I finally found what I needed here: How to get current term in my custom taxonomy in WordPress? the last update at the bottom courtesy of @user3208: <?php // Get terms for post $terms = get_the_terms( $post->ID , 'oil' ); // Loop over each item since it's an array if ( $terms != null ){ foreach( $terms as $term ) { // Print the name ...


4

/** * Get top level term */ function get_top_level_term($term,$taxonomy){ if($term->parent==0) return $term; $parent = get_term( $term->parent,$taxonomy); return get_top_level_term( $parent , $taxonomy ); }


4

The above solutions work correctly but aren't exactly optimized. Here's a method that queries that database ONCE for the data you're looking for, and gives you a list of term objects. It can accept a post_type argument, and if one is not supplied, will use the post_type of the current global $post object. function post_type_tags( $post_type = '' ) { ...


4

If you already have the term, like the term is an actual object you could use $term->parent. Otherwise you can do something like this: $term = get_term($id, 'YOUR_TAXONOMY_HERE'); $termParent = ($term->parent == 0) ? $term : get_term($term->parent, 'YOUR_TAXONOMY_HERE'); The 2nd portion of this is a shorthand if-else, IF it doesn't have a parent, ...


4

I had the same problem and I solved easily. Check this out: Define $taxonomy. It can be the slug of the taxonomy you want to get the data. After doing this, you can simply do this: <?php $postterms = wp_get_post_terms($post->ID, $taxonomy); // get post terms $parentId = $postterms[0]->parent; // get parent term ID ...


3

Use the terms_clauses filter, which passes all the various components of the query (fields, join, where, orderby, order & limits), and implement your own "search" argument: function wpse_178511_get_terms_fields( $clauses, $taxonomies, $args ) { if ( ! empty( $args['surname'] ) ) { global $wpdb; $surname_like = $wpdb->esc_like( ...


3

Inside the loop you can make use of wp_get_post_terms() to return all the terms associated with the specific post. This object returned holds all the terms with all the properties of the specified terms With that in mind, you can use a foreach loop to get each term, and then check each term's parent. For grandchild terms you will need to do some extra ...


3

This could be done in the template that displays your archive page when only one post is returned by wp_query for a given category. Something like this: if ( $wp_query->post_count == 1 && $wp_query->max_num_pages == 1 ) { wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); exit; }


3

FINAL EDIT From comments Unfortunately when I try method one, all links go back to the homepage As I stated, everything works on my test install. I actually forgot that your code works for you, because you have hardcoded the URL <a href='/?$tax=$slug' ><h5> $term->name </h5></a> What this tells me is that get_term_link() ...


3

wp_terms_checklist early trigger a filter 'wp_terms_checklist_args' so you can use that filter to add your filter and auto-remove it haver 1st run. This should be enough, however, once hooks are global variables, to be sure is better use some stronger check, a static variable inside a function is a simple and nice trick: function switch_terms_filter( $_set ...


3

Your code is correct, well almost correct. On first view, I must confess, I missed it too. You have two syntax errors in your code. If you look closely, 'parent ' and 'parent' is not the same. You should not leave blank spaces between single quotes (') and arguments. Also, you don't need to add a , after your last argument. This should work $cat_args = ...


3

This is more of a PHP question, but the solution is simple - you need to use a foreach-loop on $getslug, because you just echo the slug of the first taxonomy. The function wp_get_post_terms() does not return a single object, but an array of objects. You are right with the [0], this indicates that you are checking the first entry of said array. Your ...


3

get_terms just returns an array of terms, it doesn't generate output. You have to do something with that array to see the results- $categories = get_terms( "category" ); echo "<ul>"; foreach ( $categories as $category ) { echo "<li>" . $category->name . "</li>"; } echo "</ul>"; See the other examples on the Codex page.


3

The only alternative I know of (using core functions) is: // Get terms whose name begins with "my_name" get_terms( 'category', array( 'name__like' => 'my_name' ) ); // Get terms whose name contains "my_name" get_terms( 'category', array( 'search' => 'my_name' ) ); If you need an exact match, you'll have to execute a custom query. ...


3

I guess this function is what you are looking for -> get_ancestors()



Only top voted, non community-wiki answers of a minimum length are eligible