How to redirect custom post type archive to first term of associated taxonomy?

If you know you’re loading the right template, redirect to your first term of the specified taxonomy: function wpse_135306_redirect() { $cpt=”countries”; if (is_post_type_archive($cpt)) { $tax = ‘cities’; $args = array( ‘orderby’ => ‘id’, // or ‘name’ or whatever ); $cities = get_terms($tax, $args); if (count($cities)) { wp_redirect(get_term_link(reset($cities), $tax)); exit(); } } } // function wpse_135306_redirect … Read more

Dynamically create/remove terms in taxonomy when custom post type is published/trashed

Ok, I think I’ve figured out a viable solution. Kinda disappointed that there’s no way I’ve found thus far to hook directly into trash_{custom_post_type} like I was able to on the publish_{custom_post_type} hook. Here’s a solution for anyone else struggling with this issue. If anyone has any better suggestions, please feel free to share! /** … Read more

How do I add customize_register action AFTER adding a custom taxonomy

AFAIK it shouldn’t be a problem to get taxonomy data at the customizer_register hook state. So that problem might have different reasons, but they aren’t apparent from your question. Besides I’m not sure what you are trying to achieve, so you might want to fill the information gaps according to @Rarst’s questions. Aside from that … Read more

get_terms custom order

This can be easily done with your own custom function. What you want to do here is, get your object from get_terms() which will hold your term objects, check if your unique key is set as a parameter and then according to that, remove that key/pair and add it to the back Now, lets put … Read more

How loop through posts based on custom fields

To select posts that have a meta value, use the EXISTS comparison operator. $args = array( ‘post_type’ => ‘jobs’, ‘posts_per_page’ => 12, ‘paged’=> $paged, ‘tax_query’ => array( array( ‘taxonomy’ => ‘job_category’, ‘field’ => ‘slug’, ‘terms’ => $term->slug, ) ), ‘meta_query’ => array( array( ‘key’ => ‘_shop’, ‘compare’ => ‘EXISTS’ ) ) );

How to handle “the_terms” inside loop

To answer your first question What is the difference between those functions get_terms() returns an array of terms objects that belongs to a specific taxonomy get_the_terms() returns an array of terms belonging to a post the_terms() displays an HTML formatting string of term names belonging to a post Because you need your terms not hyperlinked … Read more

Custom taxonomy template not working with simple loop. Multiple CPT using the same taxonomy

WP defaults to showing normal native Posts in archives. It won’t automagically pick up which post types you want in your archive. You will have to adjust main query for it to explain that to it, with something like: add_action( ‘pre_get_posts’, function ( WP_Query $query ) { if ( $query->is_main_query() && $query->is_tax( ‘department’ ) ) … Read more