WPML display posts of the certain language - php

I have such a problem. On my WordPress site, I use WPML. I have several categories of posts. For each category I have a page template, this is not category.php. In order to display all posts on the page with pagination I use WP_Query. It looks like this.
...
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'cat' => 41,
'posts_per_page' => 10,
'paged' => $paged
));
?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php get_template_part('template-parts/content', 'record', get_post_format()); ?>
<?php endwhile; ?>
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 1,
'mid_size' => 2,
'prev_next' => true,
'prev_text' => '<svg class=\'pagination__previous\'><use xlink:href=' . get_template_directory_uri() . '/img/svg/sprite.svg#menu__arrow' . ' ></use></svg>',
'next_text' => '<svg class=\'pagination__next\'><use xlink:href=' . get_template_directory_uri() . '/img/svg/sprite.svg#menu__arrow' . ' ></use></svg>',
'add_args' => false,
'add_fragment' => '',
));
?>
<?php wp_reset_postdata(); ?>
...
So cat=41 is ID of a category. And on default RU language the posts are displayed as it is expected. But when I switch to other languages EN or UK (in my situation) the page with posts is empty. Can you advise me how to fix it?

Try Suppress Filter not tested it but it will help.
$query = new WP_Query( array(
'cat' => 41,
'posts_per_page' => 10,
'paged' => $paged
'suppress_filters' => false,
));

I have found a solution. But it looks strange. No need in suppress_filters. When I select cat 41 only default language is selected other languages are not displayed. BUT if I will select 44 (the same category but in non-default language). I mean that category "News" in default Russian has ID 41 (slug: news-ru) and this category in English has ID 44 (slug: news-en). So, if I select 44 everything works fine. When I am in Default the post in default are displayed. When I switch to English the posts are displayed in English and more )) when I switch to Ukrainian (slug: news-uk, ID: 49) the third language all posts are displayed in Ukrainian. Magic? Has anyone explanation?

Related

WP_Query pagination on a custom endpoint in WooCommerce

I have created a class that creates a custom endpoint for WooCommerce My Account and on this endpoint I am trying to add the lists of products with pagination using WP_Query.
Here is the code:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new \WP_Query( $args );
while( $query->have_posts() ):
$query->the_post();
echo get_the_title().'<br>';
endwhile;
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
The pagination links appear just fine and the correct number of page links.
The problem is that the get_query_var('paged') is always 0.
I checked using var_dump get_query_var('paged'). I am guessing that it has something to do with the fact that I am creating the WP_Query instance on an endpoint.
Anyone have any ideas if WP_Query with pagination can work on a custom endpoint?
After getting some good sleep, I came back to this issue this morning and with fresh eyes and I could see what was causing the issue.
As this was a custom endpoint the 'paged' variable was being added to the custom endpoint query variable, in this case called 'listings'.
If I var_dump the listings query variable I get page/2 etc. So I wrote
if( empty( get_query_var('listings') ) ):
$paged = 1;
else:
$paged_array = explode('/',get_query_var('listings'));
$paged = $paged_array[1];
endif;
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'paged' => $paged
);
This allowed me to get a paged variable and now the pagination works.
Seems like a bit of a hack, but not sure how else to get a paged variable from an endpoint

Fix pagination after Wordpress 5.5 update

After updating my website to Wordpress 5.5 pagination broke.
If example.com/news/ is the news page for my website, then following example.com/news/2/ (that opened the second page of posts before the update) redirects me to example.com/news/.
It is a known problem of the 5.5 update, but I didn't manage to fix it by changing the variable's name.
Here is how I use pagination on my website:
The query part:
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$query = new WP_Query( array(
'posts_per_page' => 5,
'paged' => $page
));
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
The pagination part:
echo paginate_links( array (
'base' => get_permalink( get_option( 'page_for_posts' ) ) . '%_%',
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'page' ) ),
'format' => '%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => '←',
'next_text' => '→',
'add_args' => false,
'add_fragment' => '',
));
wp_reset_postdata();
Using either of this:
remove_filter('template_redirect', 'redirect_canonical');
remove_action('template_redirect', 'redirect_canonical');
causes 404 error at example.com/news/2/.
Temporary fix without touching the core: disable the 404 handling for paged urls via filter pre_handle_404 in your theme's functions.php:
function pre_handle_404($preempt, $wp_query)
{
if (isset($wp_query->query['page']) && $wp_query->query['page']) {
return true;
}
return $preempt;
}
add_filter( 'pre_handle_404', 'pre_handle_404', 10, 2 );

Wordpress custom post type archive pagination results in 404 error

Whenever I try to access another page using the archive's pagination I get a 404 error. Apparently a well known problem but so far no solution has worked in my case. I might have been applying it wrong though.
Following advice I found online and in the codex I currently have the archive page template set up for a custom post-type like so:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'application',
'posts_per_page' => 2,
'paged' => $paged
);
$query = new WP_Query($args);
if ($query->have_posts()) { ?>
<div class="posts">
<!-- THE POSTS -->
</div>
<?php
$big = 999999999; // unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous', 'textdomain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next', 'textdomain' ) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
wp_reset_postdata();
} ?>
and have my permalinks set to /%category%/%postname%/. I understand this could be part of the problem.
And this is where I kind of get lost. What am I doing wrong? Any help would be very much appreciated.
I would change the first line to:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1 ?>
Also adding this line after that first line might help:
$number_of_posts = get_option('posts_per_page', 2);

Wordpress pagination don't let me go to page 2 (.htaccess redirection I guess)

I have pagination in my post. I did similar thing before, it worked well. Now I was rewriting code to another page and I'am stuck.
For example I have page with pagination:
http://localhost/est/witaj-swiecie/
And I click on page 2 with link:
http://localhost/est/witaj-swiecie/page/2/
And after it return's me back to:
http://localhost/est/witaj-swiecie/
Before I did it easily on another page but code was a little mess.
My code (with a little cut):
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$this->query = array (
'post_type' => 'oferta',
'post_status' => 'published',
'paged' => $paged,
'pagination' => true,
'posts_per_page' => '1',
'order' => 'ASC',
'orderby' => 'date',
);
$big = 999999999;
$post[0]['pagination'] = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'prev_text' => '« Poprzednia',
'next_text' => 'Następna »',
) );
Everything is done using Example With a Custom Query:
https://codex.wordpress.org/Function_Reference/paginate_links
Okay, I found the problem. I was trying to use those with shortcode on post, not on page. Using it on page solved my problem.

Pagination links is not working in wordpress

Pagination links are not working in my blog post in woocommerce...
when i click 2 the second page http://www.freshcropmushrooms.com.au/blog/page/2 its redirect to the http://www.freshcropmushrooms.com.au/blog/
on the otherhand next link is also not working
I use the following code in pagination.php
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $wp_query;
if ( $wp_query->max_num_pages <= 1 )
return;
?>
<nav class="woocommerce-pagination">
<?php
echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
'base' => str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) ) );
?>
</nav>
Please fix it for me
pagination doesn't work correctly with queries unless they're on a page.
you might need to re-read that part. what query_post() does is destroy the standard query.
It would be like
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'category_name' => 'News',
'paged' => $paged //replace it with your aray key
);
query_posts($args);

Categories