简体   繁体   English

在 Wordpress 上使用自定义字段列出并使用第二个自定义字段进行排序

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

I have a page where I list the items through a custom field called "country".我有一个页面,我在其中通过名为“国家/地区”的自定义字段列出项目。 This is my current code:这是我当前的代码:

<?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 ();
?>

Now I would like to add a second custom field called "order", numeric, which sorts the items listed with "country" through the new numeric custom field "order".现在我想添加第二个名为“order”的自定义字段,数字,它通过新的数字自定义字段“order”对列有“country”的项目进行排序。

How can I do?我能怎么做?

Thank you all谢谢你们

Ps Sorry my bad English, but I'm Italian;) Ps 对不起,我的英语不好,但我是意大利人;)

You have to use WordPress meta query for passing multiple custom fields values inside it.您必须使用 WordPress 元查询在其中传递多个自定义字段值。 You can use order by parameter.您可以使用按参数排序。 Please refer following example.请参考以下示例。

$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.
) );

Please try it.请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM