因此,我在url中有多个自定义参数,当查询达到3个参数时(例如。index.php?post_type=foo&<param-1>=<val-1>&<param-2>=<val-2>&<param-3>=<val-3>
) 可以有一个帖子,也可以没有。而不是显示存档(archive-foo.php
) 通过这篇文章,我希望wordpress显示文章本身(例如。single-foo.php
). 当然我可以在archive-foo.php
并从那里重定向到相应的帖子,但在这种情况下,我“浪费”了显示存档的整个查询。
那么,当归档文件只包含一条记录时,有没有办法通过操纵主查询(使用functions.php
并通过add_action(\'pre_get_posts\', \'<func-name>\')
)?
粗略示例:
add_action(\'pre_get_posts\', \'custom_func\')
function custom_func($query) {
if($query->get(\'param-1\')) {
// Change some $query params, but still show archive
if($query->get(\'param-2\')) {
// Change some $query params, but still show archive
if($query->get(\'param-3\')) {
// There is 1 post or none for sure
// Alter the query to something like
$wpdb->query(\'SELECT * FROM wp_posts WHERE param1=val1 AND param2=val2 ...\');
// force to load a single page with the results passed to $post object
}
}
}
}