Question

J'ai un type de poste personnalisé « Liste » et je veux obtenir toutes les annonces qui ont un champ personnalisé gateway_value != 'Yes' et pour les résultats par un autre champ personnalisé, location_level1_value. Je peux obtenir séparément les requêtes au travail, mais je ne peux pas les combiner:

Requête 1 (tri par emplacement):

                $wp_query = new WP_Query( array (
                    'post_type' => 'listing',
                    'post_status' => 'publish',
                    'posts_per_page' => '9',
                    'meta_key' => 'location_level1_value',
                    'orderby' => 'location_level1_value',
                    'order' => 'ASC',
                    'paged' => $paged
                    )
                 );

Requête 2 (valeur de champ personnalisé = Oui!):

                $wp_query = new WP_Query( array (
                    'post_type' => 'listing',
                    'posts_per_page' => '9',
                    'post_status' => 'publish',
                    'meta_key' => 'gateway_value',
                    'meta_value' => 'Yes',
                    'meta_compare' => '!=',
                    'paged' => $paged
                    )
                );

Combiné requête:

Je regardais les codex pour aider, mais la requête suivante ne fonctionne pas:

                $wp_query = new WP_Query( array (
                    'post_type' => 'listing',
                    'posts_per_page' => '9',
                    'post_status' => 'publish',
                    'meta_query' => array(
                        array(
                            'key' => 'gateway_value',
                            'value' => 'Yes',
                            'compare' => '!='
                        ),
                        array(
                            'key' => 'location_level1_value'
                        )
                    ),
                    'orderby' => "location_level1_value",
                    'order' => 'ASC',
                    'paged' => $paged
                    )
                );

Qu'est-ce que je fais mal avec la requête combinée?

[Mise à jour]: Alors maintenant que 3.1 a été libéré, la requête combinée ci-dessus ne fonctionne toujours pas. Je reçois des résultats, tout simplement pas correctement triés.

[Mise à jour]: var_dump($wp_query->request) donne ce qui suit:
string(527) " SELECT SQL_CALC_FOUND_ROWS wp_7v1oev_posts.* FROM wp_7v1oev_posts INNER JOIN wp_7v1oev_postmeta ON (wp_7v1oev_posts.ID = wp_7v1oev_postmeta.post_id) INNER JOIN wp_7v1oev_postmeta AS mt1 ON (wp_7v1oev_posts.ID = mt1.post_id) WHERE 1=1 AND wp_7v1oev_posts.post_type = 'listing' AND (wp_7v1oev_posts.post_status = 'publish') AND wp_7v1oev_postmeta.meta_key = 'gateway_value' AND CAST(wp_7v1oev_postmeta.meta_value AS CHAR) != 'Yes' AND mt1.meta_key = 'location_level1_value' ORDER BY wp_7v1oev_posts.post_date DESC LIMIT 0, 9"

Était-ce utile?

La solution

Vous pouvez utiliser la requête pour filtrer le contenu comme vous le souhaitez en utilisant le « meta_query » avec des options de filtrage, et pour la partie de la commande, il suffit d'ajouter / modifier les paramètres suivants:

  • 'orderby' => 'meta_value'
  • 'meta_key' => 'location_level1_value'
  • 'commander' => 'ASC

    $wp_query = new WP_Query( array (
        'post_type'      => 'listing',
        'posts_per_page' => '9',
        'post_status'    => 'publish',
        'meta_query'     => array(
            array(
                'key'       => 'gateway_value',
                'value'     => 'Yes',
                'compare'   => '!='
            )
        ),
        'orderby'  => 'meta_value',            // this means we will be using a selected 
                                               // meta field to order
    
        'meta_key' => 'location_level1_value', // this states which meta field 
                                               // will be used in the ordering, 
                                               // regardless of the filters
        'order'    => 'ASC',
        'paged'    => $paged
        )
    );
    

Autres conseils

Tout comme Jan a dit dans le nouveau WordPress 3.1, vous pouvez utiliser meta_query mais jusqu'à ce sortira, vous pouvez utiliser votre première requête à orderby et filtre à l'intérieur de votre boucle comme ceci:

 Global $my_query;
$my_query = new WP_Query( array (
                    'post_type' => 'listing',
                    'post_status' => 'publish',
                    'posts_per_page' => '9',
                    'meta_key' => 'location_level1_value',
                    'orderby' => 'location_level1_value',
                    'order' => 'ASC',
                    'paged' => $paged
                    )
                 );
while ($my_query->have_posts){
    $my_query->the_post();
              //do your loop stuff
} 

et ajoutez ce code à votre functions.php

   //join filter
         add_filter('posts_join', 'listing_join_865' );
         function listing_join_865($join){
Global$ my_query;            
if ('listing' = $my_query->query['post_type']){
                $restriction1 = 'gateway_value';
                return $join .="
                LEFT JOIN $wpdb->postmeta AS $restriction1 ON(
                $wpdb->posts.ID = $restriction1.post_id
                AND $restriction1.meta_key = '$restriction1'
                )";
             }else {
                return $join;
            }
         }
         //where filter
         add_filter('posts_where', 'listing_where_865' );
         function listing_where_865($where){
             global $my_query;
            if ('listing' = $my_query->query['post_type']){
                return $where.= " AND $restriction1.meta_value != 'yes'";
            }else{
                return $where;
            }
         }

cela devrait fonctionner.

Toutes mes excuses pour répondre à ma propre question:

Regarder [http://core.trac.wordpress.org/ticket/15031][1], semble que cela est un problème connu. Je fixe (piraté?) À travailler en utilisant post_filter, comme si (seulement pour la référence de tous ceux qui pourraient être à la recherche de la même réponse):

Dans functions.php ###

add_filter('posts_orderby', 'EV_locationl1' );
function EV_locationl1 ($orderby) {
    global $EV_locationl1_orderby;
    if ($EV_locationl1_orderby) $orderby = $EV_locationl1_orderby;
    return $orderby;
}

wp_query modifié dans le fichier modèle ###

$EV_locationl1_orderby = " mt1.meta_value ASC";

$wp_query = new WP_Query( array (
    'post_type' => 'listing',
    'posts_per_page' => '9',
    'post_status' => 'publish',
    'meta_query' => array(
            array(
                    'key' => 'gateway_value',
                    'value' => 'Yes',
                    'compare' => '!='
                    ),
            array(
                    'key' => 'location_level1_value'
            )
        ),
    'order' => $EV_locationl1_orderby,
    'paged' => $paged
    ));
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top