Append taxonomy terms as class names in markup?

Differ the output until you have a collection ready. $terms = get_the_terms( $post->ID, ‘teams’ ); // create a collection with your default item $classes = array(‘person’); foreach ($terms as $term) { // add items $classes[] = $term->slug; } // output all the classes together with a space to separate them. echo ‘<div class=”‘ . implode(‘ … Read more

tax_query shows no results if nothing is selected

So far good, add taxonomy condition by checking the suburbs and states Value. // Suburbs if( !empty( $_GET[‘suburbs’] ) ) { $suburbs = $_GET[‘suburbs’]; } // States if( !empty( $_GET[‘states’] ) ) { $states = $_GET[‘states’]; } // Query arguments. $args = array( ‘post_type’ => ‘properties’, ‘posts_per_page’ => 10, ); $taxquery = array(); // if … Read more

What is better: Custom Fields vs Custom Taxonomies with Terms for perfomance, scalability and better user experience

Finally after the holidays I returned back to this question. I chose to use Custom Taxonomies instead of Custom Fields. Here is why: It’s faster to make different queries (even if I will not have many posts) Most of these attributes are reusable (shared) They may be changed/modified easily in the future I’m not going … Read more

How to get Custom Taxonomy ID from the post ID

You can use wp_get_post_terms() which will return all the terms attached to the post, if any. Then, the first term of the array will be able to tell you which taxonomy it belongs to: global $post; $terms = wp_get_post_terms( $post->ID, array( ‘regions’, ‘sections’ ) ); if( ! empty( $terms ) && ! is_wp_error( $terms ) … Read more

How to show only one post for each categories of taxonomy of custom post that contains a specific custom field

<?php // get all terms of the `bank` taxonomy $banks = get_terms( array( ‘taxonomy’ => ‘bank’, // more arguments can be used, see (1) below the code ) ); // look through banks, picking one post from each foreach ( $banks as $bank ) { $args = array( ‘post_type’ => ‘account’, // your custom post … Read more

WP_Query use for a filter with multiple Taxonomies and Terms

Try this way: $args = array( ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘post_status’ => ‘publish’ ); if ( isset( $_POST[‘typefilter’] ) && $_POST[‘typefilter’] !== ” ) { $args[‘post_type’] = ‘post_type_x’; $args[‘tax_query’][0][‘taxonomy’] = ‘type_emp’; $args[‘tax_query’][0][‘field’] = ‘id’; $args[‘tax_query’][0][‘terms’] = $_POST[‘typefilter’]; $args[‘tax_query’][0][‘operator’] = ‘IN’; } if ( $_POST[‘typefilter’] == ”) { $args[‘post_type’] = ‘post_type_x’; } // If … Read more

Working with Next and Prev links in Single Taxonomy Templates

The next_post_link() and previous_post_link() functions take in_same_term parameter, which is set to FALSE by default. You will find a complete description of the functions here and here. in_same_term (boolean) (optional) Indicates whether previous post must be within the same taxonomy term as the current post. If set to ‘true’, only posts from the current taxonomy … Read more

Ordering Custom Post Types with WP_Query

Here’s a plugin that uses the posts_orderby filter to support a custom _post_type__in orderby in WP_Query that orders the post types by their array placements: <?php /** * Plugin Name: Support for _post_type__in orderby in WP_Query * Plugin URI: https://wordpress.stackexchange.com/a/392910/26350 */ add_filter ( ‘posts_orderby’, function ( $orderby, \WP_Query $q ) use ( $wpdb ) { … Read more