簡體   English   中英

在 Wordpress 上使用自定義字段列出並使用第二個自定義字段進行排序

[英]List with a custom field and sort with a second custom field on Wordpress

我有一個頁面,我在其中通過名為“國家/地區”的自定義字段列出項目。 這是我當前的代碼:

<?php

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$args = array (
'posts_per_page' => 24,
'paged'          => $paged,
'category_name' => 'food', 
'meta_key'       => 'country',
'meta_value'     => 'Italy'
);

$custom_query = new WP_Query( $args );

while($custom_query->have_posts()) :
$custom_query->the_post();


..............



endwhile;
wp_reset_query ();
?>

現在我想添加第二個名為“order”的自定義字段,數字,它通過新的數字自定義字段“order”對列有“country”的項目進行排序。

我能怎么做?

謝謝你們

Ps 對不起,我的英語不好,但我是意大利人;)

您必須使用 WordPress 元查詢在其中傳遞多個自定義字段值。 您可以使用按參數排序。 請參考以下示例。

$q = new WP_Query( array(
'meta_query' => array(
    'relation' => 'AND',
    'country_clause' => array(
        'key' => 'country',
        'value' => 'italy',
    ),
    'order_clause' => array(
        'key' => 'city',
        'compare' => 'EXISTS',
    ), 
),
'orderby' => 'order_clause', // Results will be ordered by 'city' meta values.
) );

請嘗試一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM