Blank pages in pagination - wordpress

My code for pagination in woocomerce is the following:
global $paged;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'hide_empty' => 1,
'depth' => 1,
'posts_per_page' => 30,
'orderby' => 'name',
'order' => 'ASC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'paged' => $paged
);
$loop = new WP_Query( $args );
echo '<div class="woocomerce-paginador">';
echo paginate_links( array(
'format' => '?paged=%#%',
'current' => max( 1, $loop->get( 'paged' ) ),
'total' => $loop->max_num_pages
) );
echo '</div>';
wp_reset_postdata();
I can not figured why but some pages are blank.
For example page one is blank, but page two has products.

Well, i figured out what was the problem.
I have products with no price, and pagination was trying to show no price products but instead it showed blank pages, because of condition i have in my code to only show priced products.
I solved the problem with a meta query.
'meta_query' => array(
array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
)
),

Related

Woocommerce products only "in stock"

There is the following code, the problem is that it displays all the goods in a row, regardless of status, but I would like to display only those that are "in stock".
function get_products($categories = array(), $product_type = 'featured_product', $paged = 1, $post_per_page = -1, $orderby = '', $order = '') {
global $woocommerce, $wp_query;
$args = array(
'post_type' => 'product',
'posts_per_page' => $post_per_page,
'post_status' => 'publish',
'paged' => $paged,
'orderby' => $orderby,
'order' => $order
); }
I'm not really sure what you're trying to do with your code, as your function and your args seem to be unrelated... but if you're just trying to get the products, try a custom loop:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
// Do stuff.
endwhile;
endif;
wp_reset_postdata();
Note that I have not tested this and you clearly have something unique going on with how you're trying to display the posts, so you may need to make a few adjustments.

WordPress pagination returning incorrect permalinks for paged links?

I've got a wp query loop and I have added the pagination_links function to return me the pagination. I have an issue where the link to the first page is returning me the current page instead of the link to the first page?
Can anyone see what is wrong with the below?
$posts_per_page = get_option('posts_per_page');
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => $posts_per_page,
'post_type' => 'post',
'paged' => $paged,
);
$articles = new WP_Query( $args );
args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $articles->max_num_pages,
'show_all' => false,
'end_size' => 1,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
);
echo paginate_links( $args );

Pagnation wordpress custom post type category page

Can't get the pagnation to work on the custom post types category page. It works when displaying the custom archive page. When I click on the pagnation it shows the posts from the first page but the URL says page=2.
This is the code i'm using in the archive-slug.php. How can I customize it to work with the taxonomy-slug.php?
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'post_type' => 'dropshippers', 'paged' => $paged ) );
$loop = new WP_Query( array( 'post_type' => 'dropshippers', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 8 ) );
if(have_posts()) : while(have_posts()) : the_post();
//Posts
endwhile; endif;
if(function_exists('wp_pagenavi')) {
wp_pagenavi( array( 'query' => $loop ) );
} else {
echo "No posts";
}
You need to add the name of you category in your query, i prefer to use get_posts for that:
<?php $args = array(
'posts_per_page' => 8,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'dropshippers',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Fullfit this two line with your args:
'category' => '',
'category_name' => '',

WordPress loop ignore first post of three separate meta_values

I have a loop that is pulling all of the news out, however there is three main stories set by ACF. These are Main, Secondary and Third. This wouldn't be a problem if there was only one post set to each field. However, the client wants to be able to just set a new Main post without having to worry about removing the old ones.
So to make that work I'm trying to get the loop to ignore the first of these three fields, while showing the rest AND the other posts that are set to 'No'.
I'm trying something like this but I just cannot see how else to do it.
$args = array(
// 'offset' => 1,
'posts_per_page' => -1,
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Secondary',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Third',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Main',
'compare' => 'NOT',
)
),
);
I know offset removes the ability to paginate which is important, but I saw https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination and also was told a way to go around this. This part is more important for the time being.
Here's how I finally got around not being able to do the above
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php
$excluded_key = "main_story";
$excluded_val = array("Main", "Secondary", "Third");
$exclude_ids = array();
?>
<?php
foreach ($excluded_val as $exclude) {
$args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'meta_query' => array(
array(
'key' => $excluded_key,
'value' => $exclude,
)
)
);
$excluded_id = get_posts($args);
foreach($excluded_id as $to_exclude) {
$exclude_ids[] = $to_exclude->ID;
}
}
?>
<?php
$args = array(
'post__not_in' => $exclude_ids,
'paged' => $paged
);
?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

Adding Pagination to Wordpress query for categories

I am trying to add pagination to a custom category list I have for a website gallery using WordPress. I have tried to follow a few guides but unable to add the paging function to my query. I have added my current queries.
Any help would be appreciated.
<?php
global $paged;
$curpage = $page ? $paged :1;
$args2 = array(
'taxonomy' => 'media-category',
//'parent' => '5',
'child_of' => 7,
'orderby' => 'date',
'order' => 'ASC',
'hide_empty' => 0,
'number' => 99,
'posts_per_page' => 3,
'paged' => $paged,
);
?>
<ul class="gallery-list small-block-grid-1 medium-block-grid-2 large-block-grid-3">
<?php
$categories = get_categories($args2);
foreach ($categories as $category) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_archive_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'media-category',
'field' => 'slug',
'terms' => array($category->name, 'featured'),
'operator' => 'AND',
),
),
);
$custom_query = new WP_Query($args);
?>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
I found the answer.
// Sets manual pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 12;
$offset = ($posts_per_page * $paged) - 12 ;
$args = array(
'orderby' => 'id',
'order' => 'DESC',
'hide_empty' => 0,
'number' => $posts_per_page,
'offset' => $offset,
);
$categories = get_terms($taxonomies, $args);
This sets the page up to have 12 items per page and offsets the page number.

Resources