Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 
share|improve this question
    
What's the output of printf( '<pre>%s</pre>', $articles->request ); ? –  birgire Oct 22 '13 at 11:15
1  
based on your edit, your query appears correct. do the pages have a date meta key associated with them? –  Milo Oct 22 '13 at 15:54
    
Milo, thanks for pointing that out. I obviously missed removing this... Works without the sort order! –  Staffan Estberg Oct 23 '13 at 5:27

1 Answer 1

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

$articles = new WP_Query( array(
    'showposts' => -1,
    'tag' => $tag,
    'meta_key' => 'date',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'post_type' => array( 'post', 'page' )
)
share|improve this answer
    
Sorry, forgot to mention that I've already tried this and it doesn't give me any results. This shouldn't be necessary however after initiating the tags_support_query function in my previous code. –  Staffan Estberg Oct 22 '13 at 14:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.