Tag Info

Hot answers tagged

9

To generalize this is issue of retrieving all terms of taxonomy A that posts with specific term of taxonomy B have. While this is not impossible in several steps and plenty of looping through posts (which will indeed be inefficient), I think it's reasonable to go through SQL for efficiency. My rough take on it would be: /** * Get all terms of $tax_to ...


4

You can do this with just get_terms - this allows you to fetch all (or some) of the terms from one (or more) taxonomies. By default it excludes 'empty' terms, so you'll need to set the arguments appropriately. //Array of taxonomies to get terms for $taxonomies = array('category','post_tags','my-tax'); //Set arguments - don't 'hide' empty terms. $args = ...


3

like other times, after posting here i found my own question. Maybe its not the smarter solution but its working for me. Im using the new tax_query as commented in http://www.wpmods.com/query-multiple-taxonomies-in-wp-3-1/ Basically if i get 2 taxonomies with two terms for example $job_type='full-time+free-lancer'; $job_cat='designer+programmer'; I do ...


3

If I follow your question correctly you could use a nested query loop, that is looping through your taxonomy terms and then doing a WP_Query loop for each one. There is a more complicated approach using custom SQL and a filter but the following example is what I would go for: $terms = get_terms("locations"); $count = count($terms); if ( $count > 0 ){ ...


2

I'm not sure if this the best method, and I would like to hear some other suggestions! This will alter the template to some pre-defined template if two or more taxonomies are being queried. You can hard-code the taxonomies to check or use get_taxonomies: /* * Checks to see if more than one of (certain) taxonomies are being queried * If they are, alters the ...


2

I'm not sure that there is a way to get it to use 'OR' instead of 'AND'. Alternatively, you can do: $myquery['tax_query'] = array( 'relation' => 'OR', array( 'taxonomy' => 'regions', 'terms' => array('region1'), 'field' => 'slug', 'operator' => 'IN' ), array( 'taxonomy' => ...


2

You could take the query var and programatically expand the numbers between your minimum value and maximum value. Assuming http://tax.jenswedin.com/age/10,20/ is rewritten to http://tax.jenswedin.com/?age=10,20, then (pseudo code) $parts = explode(get_query_var('age'), ','); $min_val = $parts[0]; // Should be 10 in this example $max_val = $parts[1]; // ...


2

I am a little fuzzy on specifics of technical side, but I think general outline would be following: Interface - you will need to either submit this as form by button or JavaScript. Query variables - you will need to register custom variable(s) via query_vars filter so your custom data is not discarded from URL. Query - modify query to make complex query, ...


2

I'm sure that a custom sql query would work much better but here is an option using the WordPress Tools available //first get all categories $categories = get_terms( 'category', array( 'orderby' => 'count', )); //then create an array for easier processing foreach ( $categories as $cat ) { $slugs[] = $cat->slug; ...


2

You can try getting all the posts IDs from the first taxonomy $objects = get_posts( array( 'category' => 'history', 'numberposts' => -1, ) ); foreach ($objects as $object) { $objects_ids[] = $object->ID; } Then get the terms from the second taxonomy associated with them: $collections = wp_get_object_terms( $object_ids, 'collections' );


2

It would make more sense for the sections to be child terms below each chapter. See my answer to the similar question you referenced as well as MikeSchinkel's explanation of hierarchical taxonomies. Using the the term_id returned from get_terms use get_term_children to get the sections associated with each chapter.


2

This generalisation of above worked for me: $args = array( 'cat' = -1 ); // e.g. to get list of posts in any category $postobjs = get_posts( $args ); $postids = wp_list_pluck( $postobjs, 'ID' ); $taxonomy = 'mytax' // your taxonomy name $termobjs = wp_get_object_terms( $postids, $taxonomy ); $termlist = array_unique( wp_list_pluck( $termobjs, 'name' ) ); // ...


2

I would just retrieve an array of alphabet/taxonomy terms that have posts. You do this, but doesn't actually use it. This function will return an array of non-empty (alphabet) terms: (Transient? - unless you have multiple post types using this taxonomy, (see below) I'm not sure if much is gained from using transients - and you can just call to get_terms and ...


1

You have 'terms' => 'terms' => get_terms( 'brands', array( 'fields' => 'ids' ) ), when it should be 'terms' => get_terms( 'brands', array( 'fields' => 'ids' ) ), Unless that is a typo when adding your code example?


1

I noticed your your meta key is 'wpcf-paid' which would suggest you're using Types to create custom fields. If I'm wrong, disregard the rest of this message... I had a very similar query using a value from a custom field checkbox created with Types where I also just needed to retrieve the 1 or 0. Since Types is reluctant to make things easy outside the ...


1

Your approach isn't bad, but instead of echoing all of these out, you should store them in an array, then use array_unique to remove duplicate entries before displaying. <?php $array_out = array(); while ($the_query->have_posts()) : $the_query->the_post(); ?> $terms = get_the_terms( $post->ID, 'city'); foreach($terms as $term){ ...


1

Retrieval of multiple posts in WordPress is almost always handled by WP_Query class (or get_posts() function that wraps it). There is quite a lot of documentation and information to it, but to narrowing it down to your specific case you will need to: set up posts loop for each term pass as arguments to each loop: your post type desired number of posts ...


1

So my solution on this kind of problems where we have a very complex set of rules is to avoid doing new SQL just for my solution because if you have to explain it to a beginner it gets hairy. My solution: function get_term_union( $taxonomy, $tax_query, $post_type = 'post' ){ $args = array( 'post_type' => $post_type, 'tax_query' => ...


1

In the_loop, just add this code... <?php $cats = wp_get_object_terms(get_the_id(), 'category'); ?> Print_r $cats, it will have the list of the categories applied to this post.


1

I think you're on the right track because wp_get_object_terms() can take an array of IDs for its first argument, it's just that $wp_query is not the array you want. I can't guarantee that this is more efficient (not my area of expertise), but I believe this [partially-tested] snippet would do what you want with at least one fewer loop and no array_unique(): ...


1

This helper function will return the ID of the term you are currently viewing (from any taxonomy - you can limit just to a specific taxonomy, or the built-in tag and category taxonomies). It will return false if you are not on a taxonomy term archive page. function wpse52578_get_current_term_id(){ if( !is_tax() && !is_tag() && ...


1

When the question comes to what to use Post Type, Taxonomies, custom fields ? I find that the understanding what each one of them stands for helps make the selection easier so i use: Post types - for all major data records that need/not to be displayed or queried. Taxonomies - for grouping posts/custom records together (with children) , helps a lot in ...


1

ok, this is old and you have a solution, but i was trying to do the same thing. here is how i ended up doing it w/ SQL voodoo patched together from a couple of places, but mostly: Using wp_query is it possible to orderby taxonomy? and how to group custom post type posts by custom taxonomy terms first the query voodoo: function wpa_38075( $clauses, ...


1

I could’t make it to work. May be it is not possible. I tried a plugin to set it and I was able to achieve /%issue_name%/%post_type%/%post_id%/ but not /%issue_name%/%post_type%/%post_name%/ The plugin removes that latter one by itself. Moreover I would strongly advise against such a permalink structure as it gets very taxing on WordPress performance with ...


1

I contacted Scribu because his Query Multiple Taxonomies plugin did what I was looking to do and he pointed me in the right direction. I've got this working (in my dev environment) so I think this is it. (Scribu had these split into two functions as part of a class - I'm sure that's the "classy" (pun unintended) way to do it - but this works inline: (Posting ...


1

If you're in the loop, simply use get_categories().


1

If you're not in the loop and want to get the queried terms, then there's no native API function as well as no WP_Query or WP_Tax_Query object method available (like for example get_queried_object()). You'll have to access it directly. Example of what the tax_query looks like (provided by the OP): ["tax_query"]=> object(WP_Tax_Query)#282 (2) { ...


1

I adjusted your code a little bit to integrate the wp_query() class instead of query posts(), which is only meant for altering the main loop. You should always opt to use wp_query() when trying to create secondary loops. Since we're using wp_query(), we're also going to have to use wp_reset_postdata() instead of wp_reset_query. Im not sure if this is going ...


1

I guess its because you are trying two conditions on one taxonomy, you can allways create a custom sql query, something like this: $querystr = " SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ...


1

There is no simple way to do this via WP_Query(). Depending how many categories you have, the following may not be a good idea. If you have ~20 you may be ok. So, instead of saying "all posts excluding ones in 37", you would do "get me everything in all terms (not specifying term_id 37"). To do this you need to use a category__in for all your other ...



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