Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I create a function to filter the taxonomy query with this code

add_action('pre_get_posts', 'custom_taxonomy_query');

$option_taxposts_per_page = get_option('tax_posts_per_page');

function custom_taxonomy_query(&$query)
{
   global $option_taxposts_per_page;

    if (!is_admin() && 
        is_tax('feature') || is_tax('propertytype') || is_tax('location'))  {

            if (is_tax('feature')) {
            $browseorder = get_option('browse_feature_order');
            }
            else if (is_tax('propertytype')) {
            $browseorder = get_option('browse_propertytype_order');
            }
            else if (is_tax('location')) {
            $browseorder = get_option('browse_location_order');
            }

            switch ($browseorder) {
                case "Price Descending":
                    $metakey = '_price';
                    $order = 'DESC';
                    $orderby = 'meta_value_num';
                    break;
                case "Price Ascending":
                    $metakey = '_price';
                    $order = 'ASC';
                    $orderby = 'meta_value_num';
                    break;
                case "Date Descending":
                    $metakey = '';
                    $order = 'DESC';
                    $orderby = 'date';
                    break;
                case "Date Ascending":
                    $metakey = '';
                    $order = 'ASC';
                    $orderby = 'date';
                    break;
                case "Random":
                    $metakey = '';
                    $order = '';
                    $orderby = 'rand';
                    break;
            }

            $query->set('meta_key', $metakey);
            $query->set('post_status', 'publish');
            $query->set('orderby', $orderby);
            $query->set('order', $order);
            $query->set('posts_per_page', $option_taxposts_per_page);

    }
}

the problem is if I choose order by Price Descending and Ascending.. my custom menu navigation gone and I don't know what cause this. If I choose to order by date and random everything work just fine. Please help me with this issue.

share|improve this question
    
Can you explain your reasoning further. I'm running into this same issue when using the pre_get_posts action to sort by a specific order. –  manifestphil May 18 '12 at 2:16
add comment

1 Answer

up vote 1 down vote accepted

Solved! I was using the wrong technique to create a custom query to my taxonomy instead of using the function above I create new function that use query_vars. Here is the code

function taxonomy_posts_order($query) {
    global $browsetype;

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

    if ( $query->query_vars['taxonomy'] == $term->slug ) 
    {

            switch ($browsetype) {
                case "Price Descending":
                    $metakey = '_price';
                    $order = 'DESC';
                    $orderby = 'meta_value_num';
                    break;
                case "Price Ascending":
                    $metakey = '_price';
                    $order = 'ASC';
                    $orderby = 'meta_value_num';
                    break;
                case "Date Descending":
                    $metakey = '';
                    $order = 'DESC';
                    $orderby = 'date';
                    break;
                case "Date Ascending":
                    $metakey = '';
                    $order = 'ASC';
                    $orderby = 'date';
                    break;
                case "Random":
                    $metakey = '';
                    $order = '';
                    $orderby = 'rand';
                    break;
            }
        $query->query_vars['meta_key'] = $metakey;
        $query->query_vars['orderby'] = $orderby;
        $query->query_vars['order'] = $order;
    }
    return $query;
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'taxonomy_posts_order' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.