1

我正在尝试使用 WP_Query 和 meta_query 参数查询帖子:

$args = array(
    'post_type' => 'produkty',
    'meta_query' => array(
            'relation' => 'AND',
            array(
                    'key' => 'produkt_aktywny',
                    'value' => '1',
                    'compare' => '='),
            array(
                    'key' => 'produkt_dostepnosc',
                    'value' => '1',
                    'compare' => '=')
            )
);
$query = new WP_Query( $args );

我应该添加什么来通过其他两个键('produkt_kategoria'和'produkt_cena')对结果进行排序?WP Codex 中没有关于它的内容,但是:

"Do you know how to sort the query if the meta_value is an array? Write it here :)" 

据此,我相信按多个元值排序是可能的。

4

3 回答 3

16

我想显示来自自定义帖子类型的帖子。按自定义字段对其进行排序,并在其他自定义字段为特定值的情况下添加一个参数,以仅显示选择的帖子。

例如。选择 custom_posts where custom_field_a=xxxx 并按 custom_field_b 排序

我花了一段时间才弄清楚,在搜索它时,我一直在这个帖子上磕磕绊绊。因此,对于所有遇到相同问题的人,这里是最终完成它的(简化的)代码。

        $args = array(
            'post_type' => 'my_custom_post_type',
            'meta_key' => 'custom_field_b',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'meta_query' => array(
                    array(
                        'key' => 'custom_field_a',
                        'value' => get_post_meta(get_the_ID(),'other_field',true),
                        'compare' => '='
                    )                   

                ),

        ); 
        $loop2 = new WP_Query($args);

您会看到这是一个循环中的循环,因此该值是动态的。当然,这也可以是硬编码的。

希望它可以帮助某人!

于 2013-06-04T14:53:01.467 回答
2

我相信你必须在主 $arg 数组中使用'meta_key'。您可以按该元值排序,但我认为没有内置的按 2 个元值排序的方法。您可以按第二个字段(此处为标题)排序,而不是第二个元值字段(据我所知)。

$args = array(
    'post_type' => 'produkty',
    'meta_key' => 'produkt_kategoria',
    'orderby' => 'meta_value title',
    'meta_query' => array(
            'relation' => 'AND',
            array(
                    'key' => 'produkt_aktywny',
                    'value' => '1',
                    'compare' => '='),
            array(
                    'key' => 'produkt_dostepnosc',
                    'value' => '1',
                    'compare' => '=')
            )
);

您可能需要编写一些 SQL。在此页面上向下滚动: http ://wpquestions.com/question/show/id/1916

这也可能有帮助: https ://wordpress.stackexchange.com/questions/10941/how-do-you-use-orderby-with-meta-query-in-wordpress-3-1

于 2012-10-03T22:42:48.250 回答
0
<h3>Doing request</h3>



    <?php
        $query = new WP_Query($args);
        unset($GLOBALS['wp_query']);
        $GLOBALS['wp_query'] = $query;
        ?>

<br />
<br />
<h3>Request done but without sorting, all we need from that request is difficult mysql query $query->request. Next lets get this,modificate and do again</h3>
Principle is simple: explode and add mysql string which is needed<br />

    <?php
//****************************************************************************************
    /* THIS IS REQUEST WHICH I WRITE BY HANDS TO ATCHIVE THAT I NEED. VERY USEFULL TO DO THIS BEFO PROGRAMMING!
     * 
     * SELECT wp_posts.ID FROM wp_posts 
     * INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
     * INNER JOIN wp_postmeta featured ON (wp_posts.ID = featured.post_id) 
     * WHERE 1=1 
     * AND wp_posts.post_type = 'car' 
     * AND (wp_posts.post_status = 'publish') 
     * AND ( (wp_postmeta.meta_key = 'car_year' 
     * AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN '1900' AND '2013') ) //DO NOT LOOK AT THIS, EXAMPLE
     * AND ( (featured.meta_key = 'car_is_featured') ) 
     * GROUP BY wp_posts.ID 
     * ORDER BY featured.meta_value ASC, wp_posts.post_modified_gmt DESC LIMIT 0, 20 
     */
//step 1
    $request = str_replace("SQL_CALC_FOUND_ROWS", "", $query->request); //remove unnecessary code
//step 2 - explode to insert mysql code for featured cars
    $tmp_request_array1 = explode('WHERE 1=1', $request);
    $tmp_request_array1[0].=" INNER JOIN $wpdb->postmeta featured ON ($wpdb->posts.ID = featured.post_id) ";
    $request = $tmp_request_array1[0] . " WHERE 1=1 " . $tmp_request_array1[1]; //assemble request again
//step 3 - explode assembled request to order by meta values
    $tmp_request_array1 = explode('ORDER BY', $request);
    $tmp_request_array2 = explode($order, $tmp_request_array1[1]);
    if ($orderby == 'post_modified_gmt') {
        $request = $tmp_request_array1[0] . ' ORDER BY ' . "featured.meta_value DESC, $wpdb->posts.post_modified_gmt $order" . $tmp_request_array2[1];
    } else {
        $request = $tmp_request_array1[0] . ' ORDER BY ' . "featured.meta_value DESC, $wpdb->postmeta.meta_value $order" . $tmp_request_array2[1];
    }
//step 4 - explode assembled request again to say that featured.meta_key is  =  'car_is_featured'
    $tmp_request_array1 = explode('GROUP BY', $request);
    $request = $tmp_request_array1[0] . " AND ( (featured.meta_key = 'car_is_featured') ) " . " GROUP BY " . $tmp_request_array1[1];
//**********************************************************
//step 5 - DO OUR NEW MYSQL REQUEST, AND GET CARS ORDER WE NEED
    $request_result = array();
    $tmp = $wpdb->get_results($request, ARRAY_N);
    foreach ($tmp as $value) {
        $request_result[] = $value[0];
    }
//***

    ?>

<br />
<br />

全文阅读这里

于 2013-02-12T20:55:12.453 回答