added to post above
If possible only display posts from a combination of categories or child categories.
This is the code im using it’s fine but would love to show to posts based on a combination of categories.
<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=5&cat=9');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<div class="headline-style">
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
</div>
<div class="excerpt-content">
<?php the_excerpt(); ?>
</div>
<h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
<?php endwhile; ?>
Ok I can show all posts that have categories 9 and 7 but how do I show posts that have a combination of categories 9 and 7 only.
– my code below:
<?php $my_query = new WP_Query('showposts=10&cat=7,9'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div id="wrapper">
<?php the_title(); ?></a>
<div class="excerpt-content">
<?php the_excerpt(); ?>
</div>
<h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
</div>
<?php endwhile; ?>
See:
http://codex.wordpress.org/Class_Reference/WP_Query
Under “Multiple Category Handling”
you can use ‘cat__and’ or ‘cat__in’ I believe. The difference as I understand it is that ‘cat__in’ will not show posts that are categorized with children of the cats used where as ‘cat__and’ will (meaning if a post is categorized as ‘3’ who is a child of ‘2’ and you query ‘cat__in’ => 2, the post will still show).
Thanks bythegram
Just looked again at the codex my problem is I dont know how to integrate the code into my code above.
I have a cat of records (7) that has a child cat of Jazz (9) how do I show just a combination of just records and jazz.
I haven’t tested it but I’d guess:
first line of your code replace your existing query with:
<?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__in' => array( 7, 9 )); ?>
or
<?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__and' => array( 7, 9 )); ?>
should do the trick. I’m just not 100% which one. I’d go with ‘cat__in’ first, it seems to be more exact.
Thanks – tried adding your code just got white screen -Im just a cut and paste noob unfortunately.
ok try this:
<?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__in' => array( 7, 9 ) ) ); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div id="wrapper">
<?php the_title(); ?></a>
<div class="excerpt-content">
<?php the_excerpt(); ?>
</div>
<h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
</div>
<?php endwhile; ?>
I missed the closing ‘)’ in the two queries posted originally. Sorry about that.
Thanks and thanks again it works with the “and” your a star I love wordpress cus of guys like you 🙂
Complete code below:
Thanks bythegram
<?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__and' => array( 7, 9 ) ) ); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<div id="wrapper">
<?php the_title(); ?></a>
<div class="excerpt-content">
<?php the_excerpt(); ?>
</div>
<h5>image</h5><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="text-here" /></a>
</div>
<?php endwhile; ?>
Glad it works and thanks for the awesome complement! Not to sound crazy but without all of us (including those who ask the questions) WP wouldn’t be what it is. That’s the beauty of open-source. Share the knowledge!
Happy coding!