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.

I have a custom query returning categories, but I also need to return some pages with a particular post ids. How would I add those? They obviously have no taxonomy and are page post types.

Below is my query so far:

protected function _getPostByCategory($category) {
global $wpdb;

$result = $wpdb->get_results(" SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->terms.name = '$category'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date DESC");
share|improve this question
1  
Why must this be SQL? –  s_ha_dum Aug 28 '13 at 0:56
add comment

2 Answers 2

What about using the UNION? Just an example:

(SELECT * FROM $wpdb->posts (with JOIN and WHERE as needed))

UNION

(SELECT * FROM $wpdb->posts (with WHERE to select pages)) 

GROUP BY/ORDER BY

The select(s) must be in parenthesis to use an order by for the whole union result.

As a second option, you can use a plugin to enable the categories and tags on pages, I used it many times to classify even the pages inside a category.

The call to be fired on WordPress "init" hook is:

register_taxonomy_for_object_type('category', 'page');

and while editing the page you'll have the classical category chooser like the one of regular posts.

share|improve this answer
add comment

I'm not a MySQL query expert but a few OR statements might do the trick.

OR $wpdb->posts.ID = 2
OR $wpdb->posts.ID = 703
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'page'

So in the context of your query it's like this.

$result = $wpdb->get_results(" SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->terms.name = '$category'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'

OR $wpdb->posts.ID = 2
OR $wpdb->posts.ID = 703
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'page'

GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date DESC");
share|improve this answer
    
Thank you for your response. It is almost working, but the post ids are returning within the category responses and not separately. So for example, a category of "news" has these two posts(pages) tagged onto the end. Any suggestions as to how to separate them? –  bklynM Aug 28 '13 at 16:35
    
Sorry but that's beyond my talents. –  epilektric Aug 29 '13 at 20:33
add comment

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.