• I’m trying to query all of my custom type of “Talent” and sort them but a custom taxonomy of “Level”. The levels consists of numbers 0,1,2,3,4,5,6. (sorted descending so 6,5,4,3,2,1,0). There is also a custom taxonomy of Location, but I’m not sorting by that.

    here is my query which seems to ignore the order by part.

     $location = 'location-1';
    
      $args = "";
    
      $args = array( 
        'orderby' => 'level',
        'order' => 'DESC',
        'post_type' => 'the-talent',
        'tax_query' => array(
            array (
                'taxonomy' => 'location',
                'field' => 'slug',
                'terms' => $location,
            )
        ),
        'posts_per_page'=>-1
        );
    
      $talent = new WP_Query( $args );
          
      if( $talent->have_posts() ) {
        while( $talent->have_posts() ) {
    

    here is the level taxonomy setup

    
          $labels = array(
            'name' => _x( 'Levels', 'taxonomy general name' ),
            'singular_name' => _x( 'Level', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Levels' ),
            'popular_items' => __( 'Popular Levels' ),
            'all_items' => __( 'All Levels' ),
            'parent_item' => null,
            'parent_item_colon' => null,
            'edit_item' => __( 'Edit Level' ), 
            'update_item' => __( 'Update Level' ),
            'add_new_item' => __( 'Add New Level' ),
            'new_item_name' => __( 'New Level Name' ),
            'separate_items_with_commas' => __( 'Separate Levels with commas' ),
            'add_or_remove_items' => __( 'Add or remove Level' ),
            'choose_from_most_used' => __( 'Choose from the most used Level' ),
            'menu_name' => __( 'Levels' ),
          ); 
         
        // Now register the non-hierarchical taxonomy like tag
          register_taxonomy('level','the-talent',array(
            'hierarchical' => false,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var' => true,
            'rewrite' => array( 'slug' => 'level' ),
          ));
        }
    
    

    and the custom post type of ‘Talent’ if that helps

    
    
            $args = array(
                'supports' => $supports,
                'labels' => $labels,
                'public' => true,
                'publicly_queryable' => true,
                'query_var' => true,
                'rewrite' => array('slug' => 'the-talent'),
                'has_archive' => true,
                'hierarchical' => false,
            );
            register_post_type('the-talent', $args);
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Surprisingly, WP_Query class cannot sort posts by their assigned term names. Meta values, yes, but not term names. You’ll need your own custom SQL to do so. If you really want to use WP_Query, you could replace its normal SQL with your version in the “posts_request” filter.

    Here’s an example SQL that sorts certain post types by assigned category term names.

    global $wpdb;
    $query = $wpdb->get_results( "SELECT * FROM $wpdb->posts AS p
    	LEFT JOIN $wpdb->term_relationships AS r ON (p.ID = r.object_id)
    	INNER JOIN $wpdb->term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id)
    	INNER JOIN $wpdb->terms AS t ON (r.term_taxonomy_id = t.term_id)
    	WHERE p.post_type IN ('post', 'portfolio')
    	AND p.post_status = 'publish'
    	AND x.taxonomy = 'category'
    	ORDER BY t.name ASC, p.post_date DESC;"
    );

    Edit as desired to fit your situation. To cast term names as integers to avoid natural string sorting, add +0 to name values, so
    ORDER BY t.name+0 ASC, p.post_date DESC;

    I’m not sure which term is used when posts are assigned multiple terms, I leave further investigation to you if this is an issue for you.

Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query sorted by custom taxonomy’ is closed to new replies.