在我正在开发的网站上,我添加了标记页面的功能。然后,应在查询中使用这些标记来检索标记的所有关联页面。
在我的函数文件中-
// Add tag support to Pages
function tags_support_all() {
register_taxonomy_for_object_type(\'post_tag\', \'page\');
}
function tags_support_query($wp_query) {
if ($wp_query->get(\'tag\')) $wp_query->set(\'post_type\', \'any\');
}
add_action(\'init\', \'tags_support_all\');
add_action(\'pre_get_posts\', \'tags_support_query\');
查询-
$tag = get_query_var(\'tag\');
$articles = new WP_Query( array(
\'showposts\' => -1,
\'tag\' => $tag,
\'meta_key\' => \'date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
)
);
由于某些原因,它与我的页面与标签不匹配。我可以看到这些标签被添加到管理面板中我的标签集合下,但可能缺少一个设置,使WP只能查找匹配的帖子。
为了回答birgie的问题,我在使用printf时得到了这个结果-
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (5) ) AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\') AND (wp_posts.post_status = \'publish\') AND (wp_postmeta.meta_key = \'date\' ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC
SO网友:Maruti Mohanty
这是因为默认情况下,您没有在此处提及post\\u类型post_type
是\'post\'
因此它缺了几页,
在查询中添加以下内容
\'post_type\' => array( \'post\', \'page\' )
像这样
$articles = new WP_Query( array(
\'showposts\' => -1,
\'tag\' => $tag,
\'meta_key\' => \'date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'post_type\' => array( \'post\', \'page\' )
)