sort CPT on Custom Taxonomy Archive page

I have created a custom post type called "al_product" and attached to it a custom taxonomy called "product_category".

In my archive-product_category.php I have this (relevant) code:

            if ( $  termchildren  ) {                 // we have kids...just show the terms.                            $  i=1;                 foreach ( $  termchildren as $  child ) {                     if ($  i % 2 == 0) {                         echo '<div class="tax-entry flex">';                     } else {                         echo '<div class="tax-entry flex" style="flex-direction: row-reverse;">';                     }                     ?>                         <figure class="wp-block-media-text__media">                             <?php if ( get_field('product_category_img', $  child) ) echo wp_get_attachment_image( get_field('product_category_img', $  child), 'full' ) ;?>                         </figure>                         <div style="flex:60% 0 0;" class="wp-block-media-text__content">                             <h2><?php echo $  child->name ;?></h2>                             <p>                                 <?php                                 if (get_field('short_description', $  child )){                                      echo get_field('short_description', $  child );                                 }else{                                     echo $  child->description;                                 }?>                             </p>                             <?php                             // Get (at most) 2 "product" posts in the child term.                             $  posts = get_posts( array(                                 'post_type'      => 'al_product',                                 'posts_per_page' => 2,                                 $  taxonomy_name   => $  child->slug,                             ) );                              // If there's exactly 1 post, use the post permalink.                             if ( 1 === count( $  posts ) ) : ?>                                 <a class="button" href="<?php the_permalink( $  posts[0] ); ?>">See this Product</a>                             <?php // Else, use the term link.                             else : ?>                                 <a class="button" href="<?php echo esc_url( get_term_link( $  child, $  taxonomy_name ) ); ?>">See this Series</a>                             <?php endif; ?>                          </div>                     </div> <!-- END FLEX -->                     <?php                     $  i++;                 }             } else {                  // no kids...show the products                 echo '<div class="productCategories grid">';                 while ( have_posts() ) : the_post();                     ?>                      <div class="product_cat">                         <a href="<?php the_permalink();?>">                             <?php the_post_thumbnail('small');?>                             <h2><?php the_title();?></h2>                             <?php                             $  subtitle = get_field('subtitle');                             if ( $  subtitle ) {                              echo '<h3 class="entry-subtitle">'.$  subtitle.'</h3>';                             }                             ?>                         </a>                         <?php //if (get_field('specs')['part_number']) echo '<span>'.get_field('specs')['part_number'].'</span>';?>                     </div>                  <?php                 endwhile;                 echo '</div>';             } 

The section I’m questioning is when there are no kids so we show products.

Currently this is returning the products correctly but sorting by title. I would like to sort by menu_order.

I added this to my functions.php:

add_action( 'pre_get_posts', 'rt_change_product_sort_order');  function rt_change_product_sort_order($  query){     if(get_query_var( 'product_category' )):        //Set the order ASC or DESC        $  query->set( 'order', 'DESC' );        //Set the orderby        $  query->set( 'orderby', 'menu_order' );     endif;     }; 

To no avail. I’ve also tried replacing the "If" statement with each of these:

if(is_post_type_archive( 'product_category' )): if(get_query_var( 'product_category' )): if(get_query_var( 'al_product' )): 

What am I missing here?

Taxonomy list inside the query

Is it possible to get a list of the "terms of taxonomy" or "just used taxonomy types" defined within that archive without looking at all pages of an archive query in a loop?

What I mean is this:

Custom Taxonomy Types and their terms:

colors (red, green, orangge, yellow, pink) sizes (xs, sm, md, lg, xl) 

An archive an post_type:

posts found 100 per_page 10 

In this case, the posts in the archive may be matched to any term of any taxonomy type, or they may not contain any.

When we want to use these terms to filter, it is necessary to remove the terms that do not exist in the current archive query (100 posts).

To be more precise, considering that there are 1000s of posts and 1000s of terms, it will take up unnecessary space on the screen (negative user experience) and in fact no posts will appear when filtered.

The goal here is to get a list of term or just taxonomy types in the query with the query.

Is there a configurable setting for this? Or should a new SQL query be written? Please help me.

Get Posts by Category, Tag , and CPT Taxonomy

How to print only those posts which have Category, Tag, CPT Taxonomy.

I have tried out with this code but nothing prints. To print posts I used SS [commonposts category="2" tag="3" taxonomy="company" tax_term="4" posts_per_page="5"]

function common_cats($  att){     $  args = array(     'post_type'=> 'post',     'category'    => ['category'],     'tag'    => ['tag'],     'taxonomy'    => ['taxonomy'],     'tax_term'    => ['tax_term'],     'posts_per_page' => ['posts_per_page']      );                 $  the_query = new WP_Query( $  args );    if($  the_query->have_posts() ) :      while ( $  the_query->have_posts() ) :       $  the_query->the_post();        $  output .= "<a href=".get_permalink().">.the_title().</a>";   endwhile;      wp_reset_postdata();  else:  endif; } add_shortcode('commonposts', 'common_cats'); 

Appreciate everybody’s effort.

Taxonomy hide not working

I am trying to exclude a single term from my resource pages archive page. I have created a custom post type ‘resources’ and within that a custom taxonomy called additional_category. Within that I am tagging the post as gated or LEAP. I would like to hide the gated term from displaying on the archive page. I have the following code, but its not working as expected. Can anyone tell me where Im going wrong?

   $  some_args = array(                     'post_type'        => 'resources',                     'posts_per_page'   => -1,                     'tax_query' => array(                         array(                             'taxonomy' => 'additional_category',                             'terms' => 'gated',                             'field' => 'slug',                             'include_children' => false,                             'operator' => 'NOT IN'                         )                     ),                 );                      $  s = new WP_Query( $  some_args );                      while ( have_posts() ) :                     the_post();                          /*                      * Include the Post-Type-specific template for the content.                      * If you want to override th  is in a child theme, then include a file                  * called content-___.php (where ___ is the Post Type name) and that will be used instead.                  */                 get_template_part( 'modules/u3gm-content', get_post_type() );              endwhile;         ?> 

Cannot add custom taxonomy to specific menu

I’m trying to add a custom taxonomy to a menu, but for some reason it won’t show.

This is the custom post type that I want use to associate the taxonomy:

$  labels = array(     'name' => 'Esperienza',     'singular_name' => 'Esperienza',     'menu_name' => 'Esperienza',     'all_items' => 'Esperienza',     'add_new' => 'Aggiungi nuovo',     'add_new_item' => 'Aggiungi nuova Esperienza',     'edit' => 'Modifica',     'edit_item' => 'Modifica Esperienza',     'new_item' => 'Aggiungi nuova Esperienza',     'view' => 'Visualizza Esperienza',     'view_item' => 'Visualizza Esperienza',     'search_items' => 'Cerca Esperienza',     'not_found' => 'Nessun Esperienza trovata',     'not_found_in_trash' => 'Nessun Esperienza trovata',     'parent' => 'Esperienza genitore',     'filter_items_list'     => 'Esperienza $  s',     'items_list_navigation' => 'Navigazione',     'items_list'            => 'Lista Esperienza',     'view items'    =>    'Visualizza Esperienza',     'attributes'    =>    'Attributi Esperienza' );  $  args = array(     'labels'  => $  labels,     'public' => true,     'publicly_queryable' => true,     'show_ui' => true,     'query_var' => true,     'rewrite' => array('slug' => 'st_experiences'),     'has_archive' => true,     'hierarchical' => false,     'menu_position' => 0,     'show_in_menu' => 'edit.php?post_type=st_tours',     'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),     'taxonomies' => array('st_experience_categories') );  register_post_type('st_experiences', $  args); 

and this is the custom taxonomy:

function experience_category_post_type() {     $  labels = [         'name' => __('Categorie esperienze', 'traveler'),         'singular_name' => __('Categorie esperienze', 'traveler'),         'search_items' => __('Search Categorie esperienze', 'traveler'),         'popular_items' => __('Popular Categorie esperienze', 'traveler'),         'all_items' => __('All Categorie esperienze', 'traveler'),         'parent_item' => null,         'parent_item_colon' => null,         'edit_item' => __('Edit Categorie esperienze', 'traveler'),         'update_item' => __('Update Categorie esperienze', 'traveler'),         'add_new_item' => __('Add New Categorie esperienze', 'traveler'),         'new_item_name' => __('New Experience Type Name', 'traveler'),         'separate_items_with_commas' => __('Separate Categorie esperienze with commas', 'traveler'),         'add_or_remove_items' => __('Add or remove Categorie esperienze', 'traveler'),         'choose_from_most_used' => __('Choose from the most used Categorie esperienze', 'traveler'),         'not_found' => __('No Categorie esperienze.', 'traveler'),         'menu_name' => __('Categorie Esperienze', 'traveler'),     ];      register_taxonomy('st_experience_categories', array('st_experiences'), array(         'labels' => $  labels,         'show_ui' => true,         'show_admin_column' => true,         'query_var' => true,         'rewrite' => array('slug' => 'st_experience_categories'),         'show_in_menu' => 'edit.php?post_type=st_tours',         'show_in_nav_menus' => true,         'hierarchical' => true     )); }  add_action('init', 'experience_category_post_type'); 

when I visit my custom post I correctly see the taxonomy on the right menu:

enter image description here

but on the left side menu I can’t get displayed:

enter image description here

what I did wrong?

How to arrange custom posts in the backend/admin by custom taxonomy

I’ve got a plug-in that uses a custom post type which allows custom taxonomies. The post type is employees and the the custom taxonomy is company. What I want to do is group all the employees(custom post) to the companies they are apart of, they can only be assign to one company. Is this possible? I can do easily front end through a template. I can’t work out how to create a sorting ability in the admin par the date.

rewrite URL based on selected taxonomy for post page

I have a taxonomy called "group", and their values like "red", "blue", "green" (these are generating dynamic" no any fixed group value.

during post publish I am selecting "Group" for each post. 1 group for each post.

I want to add a rewrite URL for the post if the taxonomy is "red" or "blue" or "green" then the URL of that post should come like

"myweb.com/red/post-slug-name" or "myweb.com/green/post-slug-name" or "myweb.com/blue/post-slug-name"

add_action( 'init', 'create_group_tax' ); function create_group_tax() {     register_taxonomy(              'group',              'post',               array( 'label' => 'Group',                      'hierarchical' => true                 )         ); }  add_rewrite_rule('^red/([^/]*)/?','index.php?group=$  matches[1]','top'); 

also, I want to make it regex rule "'^red/([^/]*)/?'" dynamic with selected taxonomy,this time just to make a test i added it as a static string.

result : Page not found. Nothing here – http://localhost/wordpress/red/my-test/

what do you suggest to do this?

wp_remove_object_terms correct way to call child of parent taxonomy in woocommerce

In Woocommerce I am currently able to assigned and unassign a product taxonomy from a product by calling the following.

// to assign wp_add_object_terms ( $  product_id,'in-stock', 'product_cat' );  //to unassign wp_remove_object_terms ( $  product_id,'in-stock', 'product_cat' );  

However how would one achieve this with a child of the parent taxonomy.

Do I need to call the parent and then the childs tag_ID ? Maybe something like below. Can someone clarify how I should correctly format ‘pa_stock&tag_ID=27’ please.

wp_remove_object_terms ( $  product_id,'in-stock', 'pa_stock&tag_ID=27' );  

Just to clarify pa_stock represents the parent taxonomy, tag_ID 27 represents the child taxonomy.

menu for custom type and taxonomy at the same time or filter by type

I have two custom types (eg: Cooks and Restaurants) and both have a shared taxonomy (eg: specialty). I want a menu that pulls all the restaurants based on a specific specialty but there doesn’t appear to be a way to use appearance -> menu to add the specialty taxonomy so that it only links to restaurants but not to cooks. eg: a menu that basically does a search for one type with that taxonomy match but doesn’t also pull the other type with that taxonomy match

To be clear I can get the taxonomy with both by simply editing a menu, placing a check-mark in the taxonomy name in screen options and then adding the individual taxonomy terms… I just don’t see how to filter the results to be only a single type. I assume that I will need to do this with some code but wanted to be sure I wasn’t over-engineering it.

Is there a way that is built into WordPress that would allow this to be done or am I looking at writing some custom code/adding a plugin? Alternately… what keywords should I have used to find an answer to this? It seems like anything that is remotely related is dealing with using a taxonomy with the same name as a type. I also looked at the title (at a minimum) of all the advanced-taxonomy-queries tagged questions when I discovered that but none seemed to be like my issue (though many were interesting in other ways).