web-dev-qa-db-ja.com

Pre_get_postsでmeta_keyを追加するとナビゲーションが消える

カスタムフィールドに基づいて投稿をソートできるようにするカスタム関数を書いています。ユーザーが "並べ替え"フォームを送信すると、投稿を並べ替えるためのmeta_key(orderbyとorder)パラメータが$ _SESSION変数に格納されます。

私は自分の関数にフックするためにpre_get_postsアクションを使っています。この関数は$ _SESSION変数が存在することを確認し、必要に応じて適切な値を設定しようとします。

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_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

私の直感では、この問題は この類似の質問 に関連している可能性があると言われていますが、それ以外の点ではかなり迷っています。

また、表示されている投稿に「ml_property_status」分類法による「市場外」のスラッグが適用されていないことを確認します。

関数の次の行をコメントアウトすると、すべてが正しく表示されます(並べ替えなし)。

$query->query_vars['meta_key'] = $_SESSION['ml_user']['orderby']['meta_key'];
4
Philip Downer

$query->is_main_query() codexページ の例のようにifステートメントをis_main_query()を使用するように変更した場合、問題は解決しますか?

ナビゲーションメニューはクエリによって作成されるため、通常、この問題はpre_get_postsがmenuクエリに干渉していることが原因です。それで、私はそのチェックがうまくいかなかったかどうか疑問に思う。

14
mrwweb