多个元框的Wordpress查询和按日期排序

时间:2012-02-23 17:49:57

标签: php mysql wordpress

所以这是我的问题:

$args = array( 
    'post_type' => 'Event',
    'posts_per_page' => 1000,
    'meta_key' => 'event_informations_show_on_the_homepage',
    'meta_value' => 'Show on the homepage',
    'meta_compare' => '==',
    'meta_key' => 'event_informations_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

$loop = new WP_Query( $args );

我想选择所有包含元数据event_informations_show_on_the_homepage的帖子和元数据event_informations_show_on_the_homepage的值,并按日期元数据排序,该元数据存储为时间戳,称为event_informations_date

我做错了什么?

1 个答案:

答案 0 :(得分:2)

希望我不会在这里咆哮错误的树。

您可以使用键' meta_query '按多个元键过滤帖子,如下所示:

$args = array(
'post_type' => 'Event',
    'posts_per_page' => 1000,
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
                'relation' => 'OR',
        array(
                'key' => 'event_informations_show_on_the_homepage',
                'value' => 'yes',
        ),
        array(
                'key' => 'event_informations_date',
                'value' => 'yes',
        )
     )
);
$query = new WP_Query( $args );

WordPress在这里做的是通过在同一个表上使用innerjoins来创建针对同一列的多个轮次,每次使用不同的别名。这很酷很酷可能是最快的查询方式。

有关详细信息,请参阅此处:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

希望这会有所帮助:)

相关问题