web-dev-qa-db-fra.com

L'ajout de meta_key via pre_get_posts entraîne la disparition de la navigation

J'écris une fonction personnalisée qui permet de trier les articles en fonction d'un champ personnalisé. Lorsque l'utilisateur soumet le formulaire "trier par", les paramètres meta_key (ainsi que orderby et order) utilisés pour trier les publications sont stockés dans une variable $ _SESSION.

J'utilise l'action pre_get_posts pour intégrer ma fonction. La fonction vérifie que la variable $ _SESSION existe et tente de définir les valeurs appropriées en fonction des besoins.

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

Le problème avec cette fonction est qu’elle empêche le menu de navigation principal de WordPress de paraître. Aucune erreur PHP _ n'est générée. Effectuer un dump de la variable globale $ wp_query génère les éléments suivants:

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

Mon instinct me dit que le problème peut être lié à cette question similaire , mais je suis plutôt malchanceux autrement.

La fonction vérifie également que les messages affichés ne se voient pas appliquer le slug "hors marché" de la taxonomie "ml_property_status", ces éléments ne devant jamais être affichés publiquement.

En commentant la ligne suivante dans la fonction, tout s'affiche correctement (tout simplement pas de tri)

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

Si vous modifiez votre instruction if pour qu'elle utilise $query->is_main_query() comme dans l'exemple de la page de codex is_main_query() , cela résout-il le problème?

Les menus de navigation sont créés par une requête. Ce problème provient donc généralement du fait que pre_get_posts interfère avec la requête des menus. Par conséquent, je me demande si ce chèque ne fonctionne pas.

14
mrwweb