【发布时间】:2016-02-22 14:38:54
【问题描述】:
我在我的 wordpress 安装中定义了多个自定义帖子类型。我想从所有自定义帖子类型中获取最新帖子。我查看的所有资源和教程仅描述了从一种自定义帖子类型获取最新帖子,而不是多个。
有什么办法吗?例如为 WP_Query 对象或 wp_get_recent_posts() 函数中的 post_type 属性分配多个值?如果答案是肯定的,该怎么做。
任何帮助将不胜感激。
【问题讨论】:
标签: wordpress
我在我的 wordpress 安装中定义了多个自定义帖子类型。我想从所有自定义帖子类型中获取最新帖子。我查看的所有资源和教程仅描述了从一种自定义帖子类型获取最新帖子,而不是多个。
有什么办法吗?例如为 WP_Query 对象或 wp_get_recent_posts() 函数中的 post_type 属性分配多个值?如果答案是肯定的,该怎么做。
任何帮助将不胜感激。
【问题讨论】:
标签: wordpress
它将从多个自定义帖子类型中获取帖子。
query_posts( array(
'post_type' => array( 'custom_post1', 'custom_post2', 'custom_post3',
'custom_post4' ),
'cat' => 3,
'showposts' => 5 )
);
【讨论】:
我想在这里澄清的第一件事我不了解 WordPress,但我可以帮助您处理 SQL 查询 我假设您的表中有日期时间列。
select * from table name
where column name in (here write all your different types followed by comma )
order by your date time column name desc;
例如
select * from posts where type in(1,2,3,4) order by created_on desc;
【讨论】:
让我们获取您所有的自定义帖子类型。
$args = array('public' => true, '_builtin' => false);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
$post_types 现在是一个包含所有自定义帖子类型名称的数组。你的所有帖子查询应该是这样的
$allposts = array( 'posts_per_page' => -1,
'post_type'=> $post_types,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query( $allposts );
if ( $query->have_posts() ) :
while ($query-> have_posts()) : $query -> the_post()
the_permalink();
the_title();
the_content();
endwhile;
endif;
【讨论】: