Custom Taxonomy and tax_query Issue?

Custom Taxonomy and tax_query Issue?

I am working on Plugin development and my plugin name is plugindev.I have a custom post type called team.I have a custom taxonomy Team_Category which is being registered by this code
/***************************taxonomy****************************/
add_action( 'init', 'create_team_taxonomies', 0 );
function create_team_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Team_Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Team_Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Team_Categories' ),
        'all_items'         => __( 'All Team_Categories' ),
        'parent_item'       => __( 'Parent Team_Category' ),
        'parent_item_colon' => __( 'Parent Team_Category:' ),
        'edit_item'         => __( 'Edit Team_Category' ),
        'update_item'       => __( 'Update Team_Category' ),
        'add_new_item'      => __( 'Add New Team_Category' ),
        'new_item_name'     => __( 'New Team_Category Name' ),
        'menu_name'         => __( 'Team_Category' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => false,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'Team_Category' ),
    );

    register_taxonomy( 'Team_Category', array( 'team' ), $args );
}
/****************************taxanomy end***********************************/

But when I use a tax_query in my WP_Query, I do  not get any  posts.
here is my code

    
name; ?>
'team', 'tax_query' => array( array( 'taxonomy' => 'Team_Category', 'field' => 'slug', 'terms' => $category_name,) ) ); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); the_title(); } } wp_reset_query(); ?>

Solutions/Answers:

Answer 1:

I think that the problem is right in your code, but it's really easy to overlook. Let's take a closer look at this part:

    <div class="col-md-4">
        <?php echo $category_name = $get_categories[$i]->name; ?>
    </div>
    <?php
    $args = array(
        'post_type' => 'team',
        'tax_query' => array(
            array(
                'taxonomy' => 'Team_Category', 
                'field' => 'slug', 'terms' => $category_name
            )
        )
    );

What exactly is going on in here? You get name of term and store it as $category_name ($category_name = $get_categories[$i]->name;).

Later, you use this value as a slug ('field' => 'slug', 'terms' => $category_name).

Slug and name are different things, so there is a really big probability that your Tax_Query won't find anything (if name contains capital letter, space or any special character, slug will be different from name).

So it should work fine, if you'll change it so it looks like this:

    <div class="col-md-4">
        <?php echo $get_categories[$i]->name; ?>
    </div>
    <?php
    $args = array(
        'post_type' => 'team',
        'tax_query' => array(
            array(
                'taxonomy' => 'Team_Category', 
                'field' => 'slug',
                'terms' => $get_categories[$i]->slug;
            )
        )
    );

    $query = new WP_Query($args);

References

Loading...

Order wp_query by calculated field

Order wp_query by calculated field

Hi I'm working on a store locator. 
I have a custom post "clinics" where I save clinic details (address,.., lat, lng). Each time I publish a new clinic I save post_id, lat and lng also in a custom table "lat_lng_post".
I'm able to search clinics within a specific distance using filter post_where in this way:
add_filter( 'posts_where' , 'location_posts_where' )


    function location_posts_where( $where )
        {
            // $lat  and $ lng are latitude and longitude of the searched point
            global $wpdb;    
            $where .= " AND $wpdb->posts.ID IN (SELECT post_id FROM lat_lng_post 
                 WHERE
                 (6371 * acos( cos( radians(" . $lat . ") )
                  * cos( radians( lat ) )
                  * cos( radians( lng )
                  - radians(" . $lng . ") )
                  + sin( radians(" . $lat . ") )
                  * sin( radians( lat ) ) ) ) <= 150 
                    ORDER BY 
                        ( 6371 * acos( cos( radians(" . $lat . ") )
                  * cos( radians( lat ) )
                  * cos( radians( lng )
                  - radians(" . $lng . ") )
                  + sin( radians(" . $lat . ") )
                  * sin( radians( lat ) ) ) ) ASC)";
                        return $where;
                    }

I get the clinics within the fixed distance (150km) but not ordered according to the distance. They are shown according to the post date. 
I tried to set orderby to none:
$args = array (
    'post_type'              => 'clinica',
    'orderby'                => 'none'

);

But It didn't work. How can I force to order by that ORDER BY rule I set in the query?

Solutions/Answers:

Answer 1:

Using WP_Query, you can order the results by meta_value_num:

$args = array (
    'post_type' => 'clinica',
    'meta_key'  => 'distance_field_name',
    'orderby'   => 'meta_value_num',
    'order'     => 'DESC'
);

References

Loading...