On a site that I’m working on I’ve added the ability to tag pages. These tags should then be used in a query to retrieve all associated pages of a tag.
In my functions file –
// 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');
Query –
$tag = get_query_var('tag');
$articles = new WP_Query( array(
'showposts' => -1,
'tag' => $tag,
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
)
);
For some reason it doesn’t match my pages agains the tags. I can see that the tags are being added under my tags collection in the admin panel, but supposedly there’s a setting missing that makes WP only look for matching posts.
To answer birgie’s question, I get this result when using 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
This is because you haven’t mentioned post_type here and by default
post_type
is'post'
and hence it is missing the pages,add the below in your query
'post_type' => array( 'post', 'page' )
Like this