Thread Starter
916VT
(@vincent916)
I tried to use that to exclude the category from the source :
$args = array(
'cat' => -$post_cat ,
'posts_per_page' => 2 ,
'orderby' => 'rand'
);
Unfortunately, it breaks somehow the split of languages based on Polylang and I get a mix of content.
That’s why I’m tried the ! in_category way upper.
@916VT
The problem with your first code is that you only fetch two posts. Assuming that at least one of them is in current category there’s no way to display two posts… If both are from current category then by using if… in_category() you simply exclude all fetched posts!
In theory your second code is much better and I’d say it’s at least compliant with WordPress Codex. I’m not sure what mess can Polylang cause but I’d definitely go this way…
Good luck and if I’ll manage to find something about problems with Polylang and custom queries I’ll let you know.
A.
Thread Starter
916VT
(@vincent916)
Thanks Adam for your feedback !
Yeah, but all my tries with the second code (‘cat’ => -$post_cat) killed Polylang someway… I’m pushing the first option on my side.
Thanks again for helping π
@916VT
It’s just a “semi-blind shot” (I don’t have Polylang installed anywhere at the moment) but perhaps you try something like this?
$mylang = get_query_var( 'lang' );
$args = array(
‘cat’ => -$post_cat ,
‘posts_per_page’ => 2 ,
‘lang’ => $mylang,
‘orderby’ => ‘rand’
);`
`
Thread Starter
916VT
(@vincent916)
Hum nope, it did not work… =(
I made some tries to get the current langage from another way, but same result.
All right, I guess I’ll install polylang on my test WP and give it a try π
Thread Starter
916VT
(@vincent916)
Very nice from you π
For information, I also tried to add manually the category IDs and it still breaks Polylang posts split
$args = array(
'cat' => -78,-79 ,
'posts_per_page' => 2 ,
'orderby' => 'rand'
);
Thread Starter
916VT
(@vincent916)
I keep looking from your $mylang idea, and I got that :
$language = get_locale(); if ($language == 'fr_FR') { $mylang = fr ; } else { $mylang = en ;}
$args = array(
'cat' => -78,-79 ,
'lang' => $mylang,
'posts_per_page' => 2 ,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
It avoids the mix of languages, which is cool.
But for some reasons, it still doesn’t care about the ‘cat’ => -78,-79 , even here when I write directly the categories ids…
Thread Starter
916VT
(@vincent916)
'cat' => '-78,-79' ,
Now it seems to work just fine π
Good to know! Always happy to help π
A.