Try this:
<?php $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_category = '2' AND post_category = '3'"); ?>
<?php if($my_posts) : foreach($my_posts as $post) : setup_postdata($post); ?>
~ post-related template tags, html and other content go here~
<?php endforeach; endif; ?>
I think the first line should be changed to:
<?php $my_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_category=2 AND post_category=3"); ?>
amory,
Thank you for the suggestion. I gave it a whirl, but it produced nothing for me. I looked up the get_results command in the codex and tried out the example they gave – and it worked. I then tried to slowly modify it to bring it in line with what I wanted it to do, but it seemed to hang up on the post_category – no matter what I tried, it either outputted all categories or returned an error.
I certainly appreciate the suggestion – did you actually have this running at some point? If so, would you mind posting the full code so I could take a look?
Any other suggestions are welcome, but I’m thinking that I might just have to add another subcategory.
What exactly did you do? The code above, in some form or another does work, I just didn’t have time to test it before I posted.
I believe the field post_category
is no longer use. You have to go to *post2cat
table.
After looking in the database, I’m not sure how this works. I have a post under multiple categories and only the last category gets stored under wp_post2cat. Where then do the other category ids that a post is filed under get stored?
You should have something like the following
rel_id post_id category_id
11 8 6
12 8 5
It says that Post with id 8 is associated with category id 6 and 5
Okay, here it is. Efficiency NOT guaranteed.
<?php $my_posts = $wpdb->get_results( "
SELECT * FROM
$wpdb->posts p1,
$wpdb->posts p2,
$wpdb->post2cat c1,
$wpdb->post2cat c2
WHERE
p1.ID = p2.ID AND
p1.ID = c1.post_ID AND
p2.ID = c2.post_ID AND
c1.category_id = 6 AND
c2.category_id = 5
" );
?>
<?php if( $my_posts ) : foreach( $my_posts as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach; endif; ?>
Ah…thanks for clearing that up. I must have missed the multiple values for post_id.
Wow… thanks to both of ya for figuring that out. I had no idea that it was gonna be that complex – it seems that it should be simpler, but ah well.
I’m not sure what your code even does, alphaoide, but it works perfectly. I appreciate your help.