Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

WP_Query sorted by custom taxonomy

$
0
0

Replies: 0

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 all articles
Browse latest Browse all 59525

Trending Articles