• Resolved morten66

    (@morten66)


    Hi, i have a problem with filtering posts in blog page template, i use polylang plugin and i want to display posts by language slug and category id at one time, i.e. :

    – in blog page i display posts from category “news”, i have 3 languages on my page, each language have it’s own news category

    english – “news”
    deutsch – “neuigkeiten”
    norge – “nyheter”

    So i write this code to filter posts by language slug and category id, but i know its hard coded in wrong way, it works until i use pagination.

    <?php $currentlang = get_bloginfo('language');
    	if ($currentlang == 'en-US') { query_posts('cat=3'); };
    	if ($currentlang == 'de-DE') { query_posts('cat=56'); };
    	if ($currentlang == 'nn-NO') { query_posts('cat=58'); };
    ?>

    I have added this upper code before this posts loop in blog template:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    It works until i add pagination, then i get double quantity of posts from news category in particular language, could you tell what i’m doing wrong?. I know that problem is in IF statement, because when i cut this code, pagination works well but display all posts unfiltered (as it normally should in this situaton). It’s look like filtering IF statement doesn’t cut unneeded posts from results, it not display them, but pagination see number of all posts and gives overall quantity of pages for them, i saw it when i minimized max number of post per page in wordpress generalsettings->reading panel.

    So my final question is how to correctly filter posts by language slug and category id at one time?

    Morten66

    https://wordpress.org/plugins/polylang/

Viewing 1 replies (of 1 total)
  • Thread Starter morten66

    (@morten66)

    Ok. Problem solved, by dirty solution, but it works, just placed IF statement inside pagination code :

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	query_posts(array(
    		'post_type'      => 'post', // You can add a custom post type if you like
    		'paged'          => $paged,
    		'posts_per_page' => 10
    	));
    	$currentlang = get_bloginfo('language');
    	if ($currentlang == 'en-US') { query_posts($query_string . '&cat=3'); };
    	if ($currentlang == 'de-DE') { query_posts($query_string . '&cat=56'); };
    	if ($currentlang == 'nn-NO') { query_posts($query_string . '&cat=58'); };
    ?>

    … and called pagination function between endwhile and else but before wp_reset_query();

    <?php endwhile ?>
    	<div class = "center pagin-mrg"><?php my_pagination(); ?></div>
     <?php else : ?>
    	<p><?php __('Sorry, this page does not exist.'); ?></p>
     <?php endif; wp_reset_query(); wp_reset_postdata(); ?>

    Just post this if someone will need it in the future.
    If some one know how to do this in function or correctly, please add your solution.

    Thank You.
    Morten66

Viewing 1 replies (of 1 total)

The topic ‘Filtering posts in blog page by language and category’ is closed to new replies.