I'm writing a custom function that allows the posts to be sorted based on a custom field. When the user submits the "sort by" form, the meta_key (as well as orderby and order) parameters by which the posts should be sorted is stored in a $_SESSION variable.
I'm using the pre_get_posts action to hook into my function. The function checks that the $_SESSION variable exists and attempts to set the appropriate values as needed
add_action('pre_get_posts', 'ml_order_properties');
function ml_order_properties($query) {
//ml_is_property() function simply returns true/false
//if this is a property listing related page/archive/post
if ( ml_is_property() && is_main_query() ) {
//SET THE SORT ORDER
$query->query_vars['orderby'] = $_SESSION['ml_user']['orderby']['orderby']; //Like 'meta_value_num'
$query->query_vars['order'] = $_SESSION['ml_user']['orderby']['order']; //Like 'ASC'
$query->query_vars['meta_key'] = $_SESSION['ml_user']['orderby']['meta_key']; //Like 'property_price'
//ENSURE OFF MARKET PROPERTIES ARE NOT DISPLAYED
$args = array(
'taxonomy' => 'ml_property_status',
'terms' => array(
'off-market'
),
'include_children' => true,
'field' => 'slug',
'operator' => 'NOT IN'
);
//ALWAY ADD TO THE END OF THE EXISTING ARRAY
$tax_query = $query->tax_query->queries;
$tax_count = count($tax_query) == 1 ? 1 : count($tax_query)+1;
$tax_query[$tax_count] = $args;
$query->set('tax_query', $tax_query);
}
}
The problem with this function is that it causes the primary WordPress navigation menu to stop appearing. No PHP errors are generated. Doing a dump of the $wp_query global variable outputs the following:
Object
(
| query_vars => Array (60)
| (
| | ['ml_property_type'] = String(5) "homes"
| | ['error'] = String(0) ""
| | ['m'] = Integer(1) 0
| | ['p'] = Integer(1) 0
| | ['post_parent'] = String(0) ""
| | ['subpost'] = String(0) ""
| | ['subpost_id'] = String(0) ""
| | ['attachment'] = String(0) ""
| | ['attachment_id'] = Integer(1) 0
| | ['name'] = String(0) ""
| | ['static'] = String(0) ""
| | ['pagename'] = String(0) ""
| | ['page_id'] = Integer(1) 0
| | ['second'] = String(0) ""
| | ['minute'] = String(0) ""
| | ['hour'] = String(0) ""
| | ['day'] = Integer(1) 0
| | ['monthnum'] = Integer(1) 0
| | ['year'] = Integer(1) 0
| | ['w'] = Integer(1) 0
| | ['category_name'] = String(0) ""
| | ['tag'] = String(0) ""
| | ['cat'] = String(0) ""
| | ['tag_id'] = String(0) ""
| | ['author_name'] = String(0) ""
| | ['feed'] = String(0) ""
| | ['tb'] = String(0) ""
| | ['paged'] = Integer(1) 0
| | ['comments_popup'] = String(0) ""
| | ['meta_key'] = String(14) "property_price"
| | ['meta_value'] = String(0) ""
| | ['preview'] = String(0) ""
| | ['s'] = String(0) ""
| | ['sentence'] = String(0) ""
| | ['fields'] = String(0) ""
| | ['category__in'] => Array (0)
| | (
| | )
| | ['category__not_in'] => Array (0)
| | (
| | )
| | ['category__and'] => Array (0)
| | (
| | )
| | ['post__in'] => Array (0)
| | (
| | )
| | ['post__not_in'] => Array (0)
| | (
| | )
| | ['tag__in'] => Array (0)
| | (
| | )
| | ['tag__not_in'] => Array (0)
| | (
| | )
| | ['tag__and'] => Array (0)
| | (
| | )
| | ['tag_slug__in'] => Array (0)
| | (
| | )
| | ['tag_slug__and'] => Array (0)
| | (
| | )
| | ['orderby'] = String(14) "meta_value_num"
| | ['order'] = String(3) "ASC"
| | ['tax_query'] => Array (2)
| | (
| | | ['0'] => Array (5)
| | | (
| | | | ['taxonomy'] = String(16) "ml_property_type"
| | | | ['terms'] => Array (1)
| | | | (
| | | | | ['0'] = String(5) "homes"
| | | | )
| | | | ['include_children'] = Boolean(1) TRUE
| | | | ['field'] = String(4) "slug"
| | | | ['operator'] = String(2) "IN"
| | | )
| | | ['1'] => Array (5)
| | | (
| | | | ['taxonomy'] = String(18) "ml_property_status"
| | | | ['terms'] => Array (1)
| | | | (
| | | | | ['0'] = String(10) "off-market"
| | | | )
| | | | ['include_children'] = Boolean(1) TRUE
| | | | ['field'] = String(4) "slug"
| | | | ['operator'] = String(6) "NOT IN"
| | | )
| | )
| | ['ignore_sticky_posts'] = Boolean(0) FALSE
| | ['suppress_filters'] = Boolean(0) FALSE
| | ['cache_results'] = Boolean(1) TRUE
| | ['update_post_term_cache'] = Boolean(1) TRUE
| | ['update_post_meta_cache'] = Boolean(1) TRUE
| | ['post_type'] = String(0) ""
| | ['posts_per_page'] = Integer(2) 10
| | ['nopaging'] = Boolean(0) FALSE
| | ['comments_per_page'] = String(2) "50"
| | ['no_found_rows'] = Boolean(0) FALSE
| | ['taxonomy'] = String(16) "ml_property_type"
| | ['term'] = String(5) "homes"
| )
| tax_query Object
| (
| | queries => Array (3)
| | (
| | | ['0'] => Array (5)
| | | (
| | | | ['taxonomy'] = String(16) "ml_property_type"
| | | | ['terms'] => Array (1)
| | | | (
| | | | | ['0'] = String(5) "homes"
| | | | )
| | | | ['include_children'] = Boolean(1) TRUE
| | | | ['field'] = String(4) "slug"
| | | | ['operator'] = String(2) "IN"
| | | )
| | | ['1'] => Array (5)
| | | (
| | | | ['taxonomy'] = String(18) "ml_property_status"
| | | | ['terms'] => Array (1)
| | | | (
| | | | | ['0'] = String(10) "off-market"
| | | | )
| | | | ['include_children'] = Boolean(1) TRUE
| | | | ['field'] = String(4) "slug"
| | | | ['operator'] = String(6) "NOT IN"
| | | )
| | | ['2'] => Array (5)
| | | (
| | | | ['taxonomy'] = String(16) "ml_property_type"
| | | | ['terms'] => Array (1)
| | | | (
| | | | | ['0'] = String(5) "homes"
| | | | )
| | | | ['include_children'] = Boolean(1) TRUE
| | | | ['field'] = String(4) "slug"
| | | | ['operator'] = String(2) "IN"
| | | )
| | )
| | relation = String(3) "AND"
| )
| meta_query Object
| (
| | queries => Array (1)
| | (
| | | ['0'] => Array (1)
| | | (
| | | | ['key'] = String(14) "property_price"
| | | )
| | )
| | relation = String(3) "AND"
| )
| post_count = Integer(1) 4
| current_post = Integer(2) -1
| in_the_loop = Boolean(0) FALSE
| comment_count = Integer(1) 0
| current_comment = Integer(2) -1
| found_posts = String(1) "4"
| max_num_pages = Float(1) 1
| max_num_comment_pages = Integer(1) 0
| is_single = Boolean(0) FALSE
| is_preview = Boolean(0) FALSE
| is_page = Boolean(0) FALSE
| is_archive = Boolean(1) TRUE
| is_date = Boolean(0) FALSE
| is_year = Boolean(0) FALSE
| is_month = Boolean(0) FALSE
| is_day = Boolean(0) FALSE
| is_time = Boolean(0) FALSE
| is_author = Boolean(0) FALSE
| is_category = Boolean(0) FALSE
| is_tag = Boolean(0) FALSE
| is_tax = Boolean(1) TRUE
| is_search = Boolean(0) FALSE
| is_feed = Boolean(0) FALSE
| is_comment_feed = Boolean(0) FALSE
| is_trackback = Boolean(0) FALSE
| is_home = Boolean(0) FALSE
| is_404 = Boolean(0) FALSE
| is_comments_popup = Boolean(0) FALSE
| is_paged = Boolean(0) FALSE
| is_admin = Boolean(0) FALSE
| is_attachment = Boolean(0) FALSE
| is_singular = Boolean(0) FALSE
| is_robots = Boolean(0) FALSE
| is_posts_page = Boolean(0) FALSE
| is_post_type_archive = Boolean(0) FALSE
| query_vars_hash = String(32) "225f6b4c460d5ca9fc40d6cf79dcc662"
| query_vars_changed = Boolean(1) TRUE
| thumbnails_cached = Boolean(0) FALSE
My gut tells me that the issue may be related to this similar question, but I'm pretty much at a loss otherwise.
The function also checks to ensure that the posts being displayed do not have the 'off-market' slug from the 'ml_property_status' taxonomy applied to them as those items should never be publicly shown.
Commenting out the following line in the function makes everything display correctly (just no sorting)
$query->query_vars['meta_key'] = $_SESSION['ml_user']['orderby']['meta_key'];