Order By Post Type ThenBy Taxonomy

Assuming I have these custom post types:

  1. Product
  2. Video
  3. Article

And for Product (WooCommerce Product), we have many Categories, one of which is called "Parts".

I have this situation where I need to sort my search query using the following rule:

  1. Product (that is not Parts), then
  2. Product (Parts), then
  3. Video, then
  4. Article

That means if I search a keyword, say "engine", Products should come out first (with main products coming out before parts). Then Video and Article should come later.

Is it possible to capture such a use case?

I’ve solved the issue with "Sort By Post Type", referring to this post: Sort search results by post type

What puzzles me is, how can we sort by Post Type and then by Taxonomy, but only in the case if the post_type is Product.

Any idea?

Automatically Add Tags Taxonomy in Post from Custom field’s value

I want to automatically add my ACF custom fields value as a tags when i publish/save the post.

First Tag:

My ACF Custom field’s id is: app_name

I want to add "Download Free app_name Latest Version" as a tags automatically after publishing the post.

Second Tag:

My ACF Custom field’s id is: app_dev

I want to add "Get app_name By app_dev from Here" as a tags automatically after publishing the post.

wp_insert_term doesn’t want to enter its data into custom taxonomy

so I’m working on a code to take data from an array and put it in a custom taxonomy using wp_insert_tem, but the data won’t go in. this is the json i want to fetch, this is from a url

{ "genres": [     {       "name": "Military"     },     {       "name": "Sci-fi"     },     {       "name": "Shounen"     }   ], } 

this is the code i made

$  result = file_get_contents($  url); $  array = json_decode($  result, true); foreach($  array['genres'] as $  gen){     wp_insert_term(         $  gen["name"],         "genres",         array(             "description" => "",             "slug" => sanitize_title($  gen["name"]),         ),    ); } 

please help me

Custom Taxonomy template is not recognised

I have created a custom post type (products), and at the moment I have 2 custom taxonomies (lips and eyes).

I can get the archive to show up for ‘products’, and I can get to the URL for the archive for the categories under ‘lips’, but I cannot get the archive to show for the taxonomies.

I have been through every question I can find, and I have checked the answers against what I have done, but for the life of me I can’t figure out what is going wrong.

I have a taxonomy template, ‘taxonomy-lips.php’, but it just keeps defaulting back to my front-page.php template.

Here is my code for the post type:

function my_first_post_type() {  $  args = array(          'labels' => array(                  'name' => 'Products',         'singular_name' => 'Product',                  ),              'hierarchical' => true,     'public' => true,     'has_archive' => true,     'menu_icon' => 'dashicons-list-view',     'supports' => array('title', 'editor', 'thumbnail'),     'rewrite' => array('slug' => 'shop-products'),     'taxonomies' => array('lips', 'eyes'),          ); }     register_post_type('products', $  args);      add_action('init', 'my_first_post_type'); 

And here is the code for my taxonomy:

    function my_first_taxonomy()     {     $  args = array(          'labels' => array(                  'name' => 'Lips',         'singular_name' => 'Lips',         ),              'public' => true,      'hierarchical' => true,     'rewrite' => array('slug' => 'lips'),              );          register_taxonomy('lips', array('products'), $  args);      }     add_action('init', 'my_first_taxonomy'); 

I have reset the permalinks many times, and nothing changes.

Sorry, I know this question has been asked many times, I just can’t figure out what I have done! I’m very new to all of this, so be gentle lol, I might not understand anything too complex!

Landing in 404 Page on Navigating to Custom Taxonomy Archive

Can you please take a look at this post and let me know how I can navigate to Taxonomy URL properly? Having a CPT called movie and a Taxonomy called genre how I can navigate into archive of taxonomy.php and taxonomy-genre.php

<?php      flush_rewrite_rules( false ); function custom_post_type_movie() {     $  labels = array(         'name'                  => 'movies',         'singular_name'         => 'movie',         'menu_name'             => 'Movies',         'name_admin_bar'        => 'Post Type',         'archives'              => 'Item Archives',         'attributes'            => 'Item Attributes',         'parent_item_colon'     => 'Parent Item:',         'all_items'             => 'All Items',         'add_new_item'          => 'Add New Item',         'add_new'               => 'Add New',         'new_item'              => 'New Item',         'edit_item'             => 'Edit Item',         'update_item'           => 'Update Item',         'view_item'             => 'View Item',         'view_items'            => 'View Items',         'search_items'          => 'Search Item',         'not_found'             => 'Not found',         'not_found_in_trash'    => 'Not found in Trash',         'featured_image'        => 'Featured Image',         'set_featured_image'    => 'Set featured image',         'remove_featured_image' => 'Remove featured image',         'use_featured_image'    => 'Use as featured image',         'insert_into_item'      => 'Insert into item',         'uploaded_to_this_item' => 'Uploaded to this item',         'items_list'            => 'Items list',         'items_list_navigation' => 'Items list navigation',         'filter_items_list'     => 'Filter items list',     );     $  args = array(         'label'                 => 'movie',         'description'           => 'Post Type Description',         'labels'                => $  labels,         'supports'              => array( 'title', 'editor' ),         'taxonomies'            => array( 'genre' ),         'hierarchical'          => false,         'public'                => true,         'show_ui'               => true,         'show_in_menu'          => true,         'menu_position'         => 2,         'show_in_admin_bar'     => true,         'show_in_nav_menus'     => true,         'can_export'            => true,         'has_archive'           => true,         'exclude_from_search'   => false,         'publicly_queryable'    => true,         'capability_type'       => 'page',     );     register_post_type( 'movie', $  args );  } add_action( 'init', 'custom_post_type_movie', 0 );   function genre() {      $  labels = array(         'name'                       => 'Movie Genres',         'singular_name'              => 'Taxonomy',         'menu_name'                  => 'Genre',         'all_items'                  => 'All Items',         'parent_item'                => 'Parent Item',         'parent_item_colon'          => 'Parent Item:',         'new_item_name'              => 'New Item Name',         'add_new_item'               => 'Add New Genre',         'edit_item'                  => 'Edit Item',         'update_item'                => 'Update Item',         'view_item'                  => 'View Item',         'separate_items_with_commas' => 'Separate items with commas',         'add_or_remove_items'        => 'Add or remove items',         'choose_from_most_used'      => 'Choose from the most used',         'popular_items'              => 'Popular Items',         'search_items'               => 'Search Items',         'not_found'                  => 'Not Found',         'no_terms'                   => 'No items',         'items_list'                 => 'Items list',         'items_list_navigation'      => 'Items list navigation',     );     $  args = array(         'labels'                     => $  labels,         'hierarchical'               => true,         'public'                     => true,         'show_ui'                    => true,         'show_admin_column'          => true,         'show_in_nav_menus'          => true,         'show_tagcloud'              => true,     );     register_taxonomy( 'genre', array( 'movie' ), $  args );  } add_action( 'init', 'genre', 0 ); ?> 

In my theme directory I have 3 files for taxonomy like

enter image description here

in each of them I have code like

<?php get_header(); ?>  <main id="main" class="site-main" role="main">     <?php      if ( have_posts() ) :          while ( have_posts() ) : the_post();              // Display post content             the_title();         endwhile;      endif;      ?> </main>  <?php get_footer(); 

in both http://wpdev/genre and http://wpdev/movie/genre I am landing in 404 page

enter image description here

Associate custom field with custom taxonomy with shortcode in backend

I am trying to associate a custom field to a custom taxonomy booked_custom_calendar. Custom field here is [notifications_user_id]. All this will be executed using a shortcode. When user clicks on Generate Calendar button, a term will be created and assigned to the current user using custom field and current user’s email as value in it. In this way I can filter and show this term (booked_custom_calendar) anywhere by comparing the email of current user and value in custom field.

Please help me save this field to custom taxonomy.

function createMyCustomCalendar(){      $  current_user = wp_get_current_user();     $  custom_calendar_name = "$  current_user->display_name\'s - Calendar";          $  parent_term = term_exists( $  custom_calendar_name, 'booked_custom_calendars' ); // returns an array if the taxonomy exists     $  parent_term_id = $  parent_term['term_id']; // get the numerical value of the term          $  insert_data = wp_insert_term(     $  custom_calendar_name, // new term     'booked_custom_calendars', // taxonomy     array(     'description' => $  current_user->ID,     'slug' => '',     'parent' => $  parent_term_id     )     );     //Add new meta field and fill with user ID |     // add_term_meta($  term_id, 'term_author_id', $  current_user->ID, true);          if( ! is_wp_error($  insert_data) )     $  term_id = $  insert_data['term_id'];   // A callback function to add a custom field to our "presenters" taxonomy      // Check for existing taxonomy meta for the term you're editing       $  t_id = $  term_id; // Get the ID of the term you're editing       $  term_meta = get_option( "taxonomy_term_$  t_id" ); // Do the check   ?>      <tr class="form-field">        <td>           <input type="text" name="term_meta[notifications_user_id]" id="term_meta[notifications_user_id]" size="25" style="width:60%;" value="<?php echo $  current_user->data->user_email;?>"><br />       </td>   </tr>     <?php           //$  t_id = $  term_id;           //$  term_meta = get_option( "taxonomy_term_$  t_id" );           $  cat_keys = array_keys( $  _POST['term_meta'] );               foreach ( $  cat_keys as $  key ){                   $  term_meta[$  key] = $  _POST['term_meta'][$  key];                                    //save the option array           update_option( "taxonomy_term_$  t_id", $  term_meta );               }     echo '<script>window.location.replace("'.get_admin_url().'?page=booked-appointments");</script>';      }     function showCustomCalendarButton(){  ?>     <form method="post"> <input type="submit" name="button1"  id="btn-1" value="Generate Calendar"/></form>  <?php if(isset($  _POST['button1'])) { createMyCustomCalendar(); }  }  add_shortcode('create-custom-calendar','showCustomCalendarButton'); ?> <?php 

Hide Custom Taxonomy Editor for Specific Role(s) in WP Admin

I know you can select whether to hide or show various aspects of the custom taxonomy editor by passing arguments to the show_ui, meta_box_cb, or show_admin_column fields when registering. Something like:

register_taxonomy(     '',  //NAME OF CATEGORY     '', // NAME OF CUSTOM POST TYPE     array(         'show_ui' => true,         'meta_box_cb' => false,         'show_admin_column' => true     ) ); 

What I am wondering is how can I add a hook/filter beyond this so I check for the role of the current user and then set whether those arguments are true/false depending on the role?

I am here, but stuck:

add_action('in_admin_header', 'restrictTaxonomyEditBy'); function restrictTaxonomyEditBy() {     $  screen = get_current_screen();     $  user = get_current_user_id();     global $  post;         if(!current_user_can('administrator')) {         //ADJUST TAXONOMY PROPERTIES HERE TO HIDE/SHOW ADMIN UI     } } 

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.