Custom built review plugin - pagination not working - php

I'm currently create a plugin that takes all comments from all products and places them under the review tab for all comments.
Inside Wordpress I have the following setting checked:
Settings > Discussion > Break comments into pages with...
This enables pagination on the default comment section. I've added this code with my code but it doesn't enable pagination, even when I remove the If statement.
<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
echo '<nav class="woocommerce-pagination">';
paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
) ) );
echo '</nav>';
endif; ?>
I'm currently using this function to call all reviews into one:
function products_all_reviews(){
$args = array ('post_type' => 'product');
$comments = get_comments( $args );
wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ), $comments );
}
What needs to be done to call/enable pagination properly? This is my first time creating plugin so the help is much appreciated

Related

How do I prevent posts from randomly repeating on post archive using pagination

I added pagination to a custom post-type's archive using the paginate_links() function provided by Wordpress. The page numbers and paging itself work fine but posts start to re-appear at random whenever I go to another page.
Example:
Page 1
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Page 2
Product 7
Product 4 (duplicate)
Product 8
Product 9
Product 2 (duplicate)
Product 10
And so on. No idea if this has any significance but it's always the same couple of posts that re-appear in the lists. Even after a hard refresh page 2 from the example would still look the same.
This is the custom query I used including the loop:
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// CUSTOM QUERY ARGS
$product_args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'paged' => $paged,
'orderby' => 'date',
'tax_query' => array(
'relation' => 'AND',
)
);
// CATEGORY FILTER
if(isset($_GET['cat']) && $_GET['cat']) {
$category = $_GET['cat'];
$product_args['tax_query'][] = array('taxonomy' => 'product-category', 'terms' => $category, 'field' => 'slug', 'operator' => 'IN');
}
$product_query = new WP_Query($product_args);
// THE LOOP
if ($product_query->have_posts()) {
while ($product_query->have_posts()): $product_query->the_post();
get_template_part('template-parts/content', 'product');
endwhile;
wp_reset_postdata();
} ?>
And the paginate_links() function as documented on Wordpress Codex:
<?php
$big = 999999999; // need an unlikely integer
$total_pages = $product_query->max_num_pages;
$translated = __( 'Page', 'project' ); // Supply translatable string
$current_page = max(1, get_query_var( 'paged' ));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '<i class="far fa-angle-left"></i>',
'next_text' => '<i class="far fa-angle-right"></i>',
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
)); ?>
I basically copied it from the documentation making only necessary changes to suit my setup.
I'll also add the rewrite rule I added to functions.php for the pagination links in case this could have anything to do with the issue:
/**
* Fix pagination on archive pages
*/
function pagination_rewrite() {
add_rewrite_rule(get_option('category_base') . '/page/?([0-9]{1,})/?$', 'index.php?pagename=' . get_option('category_base') . '&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
I read that custom queries can mess up pagination functions but they have a section specifically for handling Custom Queries in the documentation and I made sure to follow it to the letter. What could I be doing wrong here?

Wordpress wp_query not showing some posts

I'm trying to get 5 posts of one category and paginate them, but my loop doesn't get some posts. In this case, I put 5 posts to retrieve but the loop only returns 2 and sometimes 3.
Finally, when I'm trying to use pagination it doesn't work.
Here is my code:
<?php
// Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'category__in' => array( 11 ),
'category__not_in' => '',
'posts_per_page' => 5,
'post_type' => 'post',
'post_status'=>'publish',
'paged' => $paged,
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$the_query->the_post();
// Post content goes here...
//
echo '
<h3 class="tituliNota">
<a href="'.get_permalink().'" class="noteTitle">
<b>'.the_title( ' ', ' ', false ).'</b></a></h3>';
get_the_category();
wp_reset_postdata();
endwhile; ?>
<div class="pagination">
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'mid_size' => 5,
'prev_text' => __('« Prev Page'),
'next_text' => __('Next Page »')
) );
?>
</div>
The main query runs before the template is loaded, and WordPress decides what template to load based on the results of that query.
You say your default posts_per_page is set to 5, and there are 2 or 3 posts in that category, so as far as WordPress knows, there is no page 2. Your custom query that you run in the template with a different posts per page setting is unrelated to the main query.
The solution is to make adjustments to the main query before the template is loaded, via the pre_get_posts action. This would go in your theme's functions.php file
function category_posts_per_page( $query ) {
if ( !is_admin()
&& $query->is_category()
&& $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
}
add_action( 'pre_get_posts', 'category_posts_per_page' );

Limit date range for related posts in Jetpack

I'm trying to apply a php code snippet to my wordpress page to enable Jetpack to limit its related posts to no later than 2017-01-01. However, when I add this code via Snippets - with Jetpack enabled to show thumbnails - Jetpack doesn't work. Is my code incorrect or is there some magic trick for this?
// related posts - don't show posts earlier than January 1 2017
function jetpackme_exclude_related_post($exclude_post_ids, $post_id ) {
$args = array(
'date_query' => array(
'before' => 2017-01-01,
'inclusive' => true,
),
'posts_per_page' => '99999',
'fields' => 'ids'
);
$query = new WP_Query($args);
if ($query->have_posts()):
foreach($query->posts as $id ):
$exclude_post_ids[] = $id;
endforeach;
endif;
return $exclude_post_ids;
}
add_filter( 'jetpack_relatedposts_filter_exclude_post_ids',
'jetpackme_exclude_related_post', 20, 2 );

Create shortcode for displaying woocommers users order history

I'm trying to write a shortcode to display users woocommerce order history.
I've found an answer here in woocommerce, is there a shortcode/page to view all orders? , but that does'nt work anymore.
If i follow the current answer it gives me a fatal error.
Fatal error: Call to undefined function wc_get_account_orders_actions() in /wp-content/themes/wrapgate/woocommerce/myaccount/my-orders.php on line 72
Anybody knows updated code to get my shortcode to work?
Here's the shortcode function i've tried
add_shortcode( 'woocommerce_history', 'woo_order_history' );
function woo_order_history() {
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', get_current_user_id() ),
'order_count' => -1
));
return ob_get_clean();
}
Same error occurs if i try to use
woocommerce_account_orders( -1 );
Woocommerce as well as wordpress are on the latest version.
I've tried to call the shortcode function from my themes functions.php
Thanks in advance for every help.
my-orders.php is deprecated after version 2.6.0. Woocommerce my-account uses orders.php now. To create a shortcode to display order history
function woo_order_history( $atts ) {
extract( shortcode_atts( array(
'order_count' => -1
), $atts ) );
ob_start();
$customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
) ) );
wc_get_template(
'myaccount/orders.php',
array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total,
)
);
return ob_get_clean();
}
add_shortcode('woocommerce_history', 'woo_order_history');
Add this code to your theme->functions.php or child-theme->functions.php (if you have child theme enabled).
Now where you want to display the order just add the shortcode [woocommerce_history]

Modifying a blog page to only display posts from same category when next/prev link is pressed

I've seen a very similar post to this and lots of other examples such as the wordpress documentation for custom functions here:
Function reference for next/prev post link
However, my example sits within what appears to be a custom function within the Storefront (woothemes) theme and so I'm having trouble incorporating the "$in_same_term = true," in the right way without breaking the functionality. Here is the (raw) bit of code in the 'post.php' file within the theme where I think I need to somehow incorporate the "$in_same_term = true,"...
if ( ! function_exists( 'storefront_paging_nav' ) ) {
/**
* Display navigation to next/previous set of posts when applicable.
*/
function storefront_paging_nav() {
global $wp_query;
$args = array(
'type' => 'list',
'next_text' => _x( 'Next', 'Next post', 'storefront' ) . ' <span class="meta-nav">→</span>',
'prev_text' => '<span class="meta-nav">←</span>&nbsp' . _x( 'Previous', 'Previous post', 'storefront' ),
);
the_posts_pagination( $args );
}
if ( ! function_exists( 'storefront_post_nav' ) ) {
/**
* Display navigation to next/previous post when applicable.
*/
function storefront_post_nav() {
$args = array(
'next_text' => '%title <span class="meta-nav">→</span>',
'prev_text' => '<span class="meta-nav">←</span> %title',
);
the_post_navigation( $args );
}
}
I'm thinking I could be close as loads of searches have revealed the same kind of info, I'm just not integrating it properly...
Thanks in advance for any suggestions on how best to incorporate this functionality!
You have just to add 'in_same_term' => true in the array of the storefont_post_nav function
function storefront_post_nav() {
$args = array(
'next_text' => '%title <span class="meta-nav">→</span>',
'prev_text' => '<span class="meta-nav">←</span> %title',
'in_same_term' => true,
);
}
Is possible to add other options in the array. You can have a look here https://developer.wordpress.org/reference/functions/the_post_navigation/
Ok, so my main problem here was trying to incorporate the code in the wrong place - all I had to do was change the next_post_link parameter in this file: wp-includes/link-template.php. (Not the post.php file as I was trying to do).
It's worth noting for each next and previous link there were 2 sets of parameters where $in_same_term = false needed to be changed to true.
Works a treat now!

Categories