Thread Starter
Alkorr
(@alkorr)
Hi! It’s been 2 months now I asked for help in this forum and since then I worked on a solution but I couldn’t make it work neither…..
I changed the query and added:
$args=array(
'posts_per_page' => 1,
'post__not_in' => $do_not_duplicate,
'post__not_in' => get_option( 'sticky_posts' ),
'category__in' => array($category->term_id),
But I still get duplicates because I have another query on my page, a different one, and the same post is showing twice…
Any help would be so great, thanks a lot! π
http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action
for ‘do-not-duplicate’ with several loops and posts, try the ‘array method’ described in the above linked docu described after ‘Note for Multiple Posts in the First Category’;
(the first loop – example):
<?php $do_not_duplicate = array();
$my_query = new WP_Query('posts_per_page=4');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
(the second loop – rough example, make sure to use the right query args):
<?php $args=array(
'posts_per_page' => 1,
'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )),
'category__in' => array($category->term_id)
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
(any further loop – rough example, the query args need adjusting):
<?php $args=array(
'posts_per_page' => 1,
'post__not_in' => array_merge($do_not_duplicate, get_option( 'sticky_posts' )),
'category__in' => array($category->term_id)
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
i hope this gives you the idea how to use the method.
if you need help with the details, please paste the full code of the template into a http://pastebin.com/ and post the link to it here.
Thread Starter
Alkorr
(@alkorr)
Hi alchymyth! Thanks, this code looks very helpful and I’m trying to replace my actual code with it. But I’m already stuck: in the first loop, how do I do to exclude all sticky posts?
My query uses:
‘post__not_in’ => get_option( ‘sticky_posts’ )
I don’t know how to make it work within your loop. I tried some code from the Codex but it shows sticky posts and doesn’t exclude them from the loop… I feel dumb for asking, sorry, but I’m learning π
Thanks!
if you need help with the details, please paste the full code of the template into a http://pastebin.com/ and post the link to it here.
please do the above, so i can see the full code.
in the first loop, try:
<?php $do_not_duplicate = array();
$my_query = new WP_Query(array('posts_per_page' => 4, 'post__not_in' => get_option( 'sticky_posts' ) ) );
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
(untested)
Thread Starter
Alkorr
(@alkorr)
Sure, here is the link: http://pastebin.com/diYiFYMD
I hope it will help you. I don’t see what I am doing wrong…
Thanks π
http://pastebin.com/J1LTBd8H
hope you can check what i’ve changed in the code;
hopefully it will work with your posts π
Thread Starter
Alkorr
(@alkorr)
Great, it seems to work! But there is a little problem: on the third loop, the latest post from each category doesn’t come from the good category. For example: the review of game appears in the category Movies, and so on. It worked fine with the old code so maybe what you changed had a side effect?
Apparently it’s working fine so far, now I only have to see the third loop working to tell you if it’s perfect π
Again, thanks a lot…
on the third loop, the latest post from each category doesn’t come from the good category.
are the posts in more than one category?
Thread Starter
Alkorr
(@alkorr)
Hi alchymyth! Nope, they are only in one category, no duplicates, no sticky so far so it works fine. But now, instead of showing:
It shows:
I looked at the code and I don’t understand why the post is not from the right category. Do you have an idea of what the problem might be?
Thanks! π
in the third loop, try to change:
foreach($categories as $category) {
$args=array(
'posts_per_page' => 1,
to:
foreach($categories as $category) {
$args=array(
'numberposts' => 1,
and please paste the full code of the third loop into a http://pastebin.com/ and post the link to it here.
Thread Starter
Alkorr
(@alkorr)
I pasted the full code of the third loop here: http://pastebin.com/7Jrssy5X
I made the change you suggested but it didn’t change anything.
Also the posts are not anymore classified by Category (Alphabetically) but by date. Maybe this is the problem here and it explains why the post about a movies is showing instead of a post about games, and so on…
Hope it helps! π
please paste the real section that you are actually using in your template.
not the one with HTML CODE HERE
in you pasted version, you are missing the ‘category__in’ parameter for the ‘get_posts()’ loops.
$args=array(
'numberposts' => 1,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
);
Thread Starter
Alkorr
(@alkorr)
the category paramter in the $args is missing;
try:
$args=array(
'numberposts' => 1,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'cetagory__in' => array($category->term_id)
);