我成功地得到了按meta_key值日期订购的CPT的所有帖子。代码如下
$today = date("Y/m/j");
query_posts(array(
'post_type' => 'event',
'posts_per_page' => 10,
'meta_key' => '_start_eventtimestamp',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_start_eventtimestamp',
'meta-value' => $value,
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
)
));
if (have_posts()) :
while (have_posts()) : the_post();现在尝试一次使用值显示一个类别。
post_type=>event,taxonomy=>event_catgory,slug=>green
How我可以在index.php上显示这些帖子,也可以按日期的meta_key值进行排序吗?
发布于 2018-02-24 06:20:29
永远不要使用query_posts!使用get_posts()或新的WP_Query()代替。
不过,我认为这是可行的:
$today = date("Y/m/j");
$args = (array(
'post_type' => 'event',
'posts_per_page' => 10,
'orderby' => array(
'wpse_meta_query_name' => 'ASC',
'post_title' => 'DESC' // optional second orderby paramater
),
'meta_query' => array(
'wpse_meta_query_name' => array( // give the meta query arguments a name, to pass into 'orderby'
'key' => '_start_eventtimestamp',
// 'meta-value' => $value, // this was in your code but should not be
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
),
'tax_query' => array(
'relation' => 'AND', // redundant default value 'AND' but I still put it in..
array(
'taxonomy' => 'event_catgory',
'field' => 'slug',
'terms' => 'green',
),
)
));
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post(); // edit: added $query
// ...
}
}编辑--编辑--
我想说更多的事情,除了在下面讨论你对分类法的评论-事件分类。
我决定忽略这个问题的一些细节,这就是您正在index.php上运行这个查询的事实
当您在该文件上时,您可以相当肯定已经执行了查询。要访问该查询,可以执行以下操作
global $wp_query
if ( $wp_query->have_posts() ) { ... etc. etc.这将与我在上面所做的相同,只是全局$wp_query中没有内置一个分类法查询,这是因为您在index.php上
但是,您不需要使用全局$wp_query。相反,当您只运行函数have_posts()和the_post()而不是$variable_is_a_wp_query->that_posts()时,它只为您引用全局$wp_query。因此,通常没有理由引用该全局变量。
如果您想修改在index.php上已经在幕后运行的查询,则应该查看“pre_get_posts”钩子。您可以完成与上面相同的事情,但方式略有不同。
但是,当您只想显示类别中的帖子时,通常不需要这样做。相反,像下面提到的那样,使用分类法-Eventclassy.php,确切的'tax_query‘将自动包含在全局$wp_query中,但现在它将根据url (即。你的网站/事件-类别/绿色,或你的网站/事件-类别/蓝色)。
另外,index.php会列出您的帖子是的,但是index.php也适用于其他的帖子类型。实际上,这就是我可能会错的地方。我的理解是,定制的post类型将使用archive.php (这与index.php非常相似),而index.php是一种奇怪的文件,因为在某些WordPress安装中它是您的主页,而在其他WordPress中,您的主页可以是前台-page.php或template- some . the,index.php只是列出所有属于“post”类型的帖子的“归档”页面(即。博客文章)。(请查看此图以获得更多信息:https://developer.wordpress.org/files/2014/10/wp-hierarchy.png)
因此,对于为什么在index.php上显示事件感到有点困惑。如果此页面实际上是您的主页,则应将其命名为front-page.php。然后在设置->读取中,选择一个类似..。“首页显示静态页面”。创建一个名为home的页面,然后在下拉菜单下选择它。这样,全局$wp_query就不会从数据库中抓取一堆博客文章,只是为了编写自己的新查询。相反,它只是查询对应于主页的单个post_id。
那么从这里到哪里。我想我的建议是尝试创建一个名为archive. try的文件。然后,如果您愿意,您可以让index.php和archive.php做他们想做的任何事情,并且您可以控制哪种post类型进入哪个归档-{post- type }.php页面。通常我就是这样做的。
如果您除了使用分类法-Eventclassy.php之外,还可以使用它,那么您几乎已经设置好了。剩下的唯一的事情是添加'meta_query‘,所以为了改变文章的顺序,而不是默认的先显示最近的帖子。
在此设置下,归档-事件. but显示所有事件,分类法-事件_分类.but显示所有事件,但已经应用了特定的分类法查询。这些页面的html可能或多或少是相同的。
因此,让我们尝试通过"pre_get_posts“来”修改主查询“,用于所有与”事件“具体相关的前端主查询。尝试将其放入您的functions.php中:
/**
* @param $query - passed by reference
*/
function wpse_modify_events_main_query( $query ) {
// this function is run on every single wp_query which happens in a ton of places
// so we need to figure out the logic required so that we don't unintentionally modify the wrong queries
// sometimes there seems to be no clear way on exactly how to do this.. different things work for different purposes
if ( is_admin() ) {
return; // in /wp-admin, do not modify
}
// queries where we manually write 'new WP_Query()' are not main queries
if ( ! $query->is_main_query() ) {
return; // not main query, do not modify
}
// there may be better ways to verify post type? but this will work.
if ( isset( $query->query['post_type'] ) && ( $query->query['post_type'] == 'event' ) ) {
// is a meta_query is already set for one reason or another, we may accidentally overwrite it.
// this may or may not be intended, so kind of tough to decide what to do if one is already in place.
// if ( $query->get('meta_query') ) {
// // return ?? probably not.
// }
$today = date("Y/m/j");
$query->set('meta_query', array(
'wpse_meta_query_name' => array(
'key' => '_start_eventtimestamp',
'value' => $today,
'compare' => '>=',
'type' => 'CHAR'
)
) );
$query->set('orderby', array(
'wpse_meta_query_name' => 'ASC',
'post_title' => 'DESC' // optional second orderby paramater
) );
}
// the $query variable was passed by reference, meaning we don't need to return the $query.
// when we do things like $query->set() it will modify the main query.
}
// run each query through our own function before it hits the database
add_action( 'pre_get_posts', 'wpse_modify_events_main_query');如果有人能告诉我,如果我在pre_get_posts调用中遗漏了什么,那就太好了。WordPress有很多功能,比如..is_post_type_archive(),is_tax(),也可以使用,但有时他们似乎没有完全按照他们说的做。
若要检查您的查询是否正在被修改,请使用全局$wp_query,然后在其上使用print_r,查看它是否附加了meta_query参数。
https://wordpress.stackexchange.com/questions/295029
复制相似问题