Adding Pagination to Wordpress query for categories - wordpress

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.

Related

Blank pages in pagination

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'
)
),

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 get Custom Post by Custom Taxonomy

I created a custom post and Custom Taxonomy
I need to search post using taxonomy
used this code but result is null
$properties = new WP_Query(
array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
)
);
I found a solution myself: issue is WP_Query() function passed valued in array
<?php
$args = array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
wp_reset_postdata();
else :
esc_html_e( 'Sorry, no posts matched.' );
endif;
?>

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' => '',

fetch products if image is found in woocommerce?

i want to show image only products in woocommerce.
My fetch query is given below
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 9, 'orderby' =>'date','orderby' => 'rand' );
$loop = new WP_Query( $args );
how can i get the image products only?
Are you looking for something like this ?
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 9, 'orderby' =>'date','orderby' => 'rand' );
$loop = new WP_Query( $args );
while ($loop->have_posts()) : $loop->the_post();
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
endwhile;
?>
UPDATED
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 9,
'orderby' =>'date',
'orderby' => 'rand',
'meta_query'=>array(
array(
'key'=>'_thumbnail_id',
'compare' => 'EXISTS'
)
)
);
Change arg to get product if it has feature image.

Resources