【问题标题】:WordPress WP_Query: Display custom post type based on custom meta value, and also order on another custom meta valueWordPress WP_Query:根据自定义元值显示自定义帖子类型,并按另一个自定义元值排序
【发布时间】:2014-07-30 11:30:39
【问题描述】:

我正在使用 WordPress 插件 Advanced Custom FieldsCustom Post Type UI

我已经构建了一个 WP_Query 来显示特定的帖子类型,由特定的自定义字段值过滤。

$loop = new WP_Query( array( 
    'post_type' => 'news', 
    'meta_key' => 'news_story_type', 
    'meta_value' => 'release', 
    'posts_per_page' => 3 
) );

我现在想按另一个自定义字段date_of_publication 对生成的帖子进行排序,而不是使用 WordPress 的 menu_order 或日期。 ACF documentation 表示在查询参数中指定 orderbymeta_key

$args = 数组(

'post_type' => '事件',

'posts_per_page' => -1,

'meta_key' => 'start_date',

'orderby' => 'meta_value_num',

'order' => 'DESC' );

但是,这样做与我已经提供给过滤器的 meta_key 冲突。

有没有人遇到过这个问题并找到了解决方案?

【问题讨论】:

标签: php wordpress custom-fields advanced-custom-fields


【解决方案1】:

尝试使用 meta_query

$loop = new WP_Query( array( 
    'post_type' => 'news', 
    'meta_key' => 'start_date',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'posts_per_page' => 3,
    'meta_query' => array(
        array('key' => 'news_story_type', 'value' => 'release')
     )
) );

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多