我正在编写一个自定义函数,允许根据自定义字段对帖子进行排序。当用户提交“排序依据”表单时,meta\\u键(以及orderby和order)参数将存储在$\\u会话变量中,根据这些参数对帖子进行排序。
我正在使用pre\\u get\\u posts操作来钩住我的函数。该函数检查$\\u会话变量是否存在,并根据需要尝试设置适当的值
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);
}
}
此功能的问题在于,它会导致WordPress主导航菜单停止显示。不会生成PHP错误。转储$wp\\u query全局变量会输出以下结果:
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
我的直觉告诉我这个问题可能与
this similar question, 但在其他方面我几乎不知所措。
该功能还检查以确保显示的帖子没有应用“ml\\U property\\U status”分类法中的“场外”slug,因为这些项目永远不应该公开显示。
注释掉函数中的以下行可以正确显示所有内容(只是不排序)
$query->query_vars[\'meta_key\'] = $_SESSION[\'ml_user\'][\'orderby\'][\'meta_key\'];