0

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.

  • 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. – Philip Downer May 18 '12 at 2:16
1

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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