wp-pagenavi not working on custom query wordpress - wordpress

I face very weird issue on wp-pagenavi plugin. I am using WooCommerce plugin it works perfectly. Now i make a page template and want to show all that products which product type is bundle, my products show but wp-pagenavi not working. I also try on blog page its working perfect there but not in my page template.
Here is my code:
Page Template Name
<?php
/*
Template Name: Bundle Products
*/
get_header();
?>
My custome Query
<?php
$paged = get_query_var('page') ? get_query_var('page') : 1;
$gb_bundle_args = array(
'post_type' => 'product',
'order' => 'DESC',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'bundle'
)
)
);
$gb_bundle_qry = new WP_Query($gb_bundle_args);
if($gb_bundle_qry->have_posts()) :
while($gb_bundle_qry->have_posts()) :
$gb_bundle_qry->the_post();
the_title();
echo '<br />';
endwhile;
else :
echo "No Bundle Products";
endif;
wp_pagenavi();
wp_reset_query();
?>
I searched alot about this but nothing found.

You need to pass
wp_pagenavi( array( 'query' => $gb_bundle_qry ) );
wp_reset_query();
Hope it helps you.

Related

Wordpress 'posts_per_page' not working on global $wp_query on front-page.php

In my Wordpress admin panel, in the section 'Settings' -> 'Reading', I have set the 'Blog pages show at most' value as 5 and I have the 'Your homepage displays' value set to 'Your latest posts'.
I am trying to bypass this on my front-page.php loop by setting the 'posts_per_page' argument of the query to 2 (or any number other than 5) and I haven't added a 'pre_get_posts' hook in my functions.php file. But the result always shows 5 posts.
This is my front-page.php file:
<?php get_header(); ?>
<?php
global $wp_query;
$wp_query->set('posts_per_page', 2);
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) {
//var_dump($wp_query->get('post_type'));
$wp_query->the_post();
the_title();
echo '<br>';
}
echo paginate_links();
}
?>
<?php get_footer(); ?>
Kindly let me know how I can bypass the value that I have set in the admin panel.
Thank You,
Clement
You can try with the below code -
$MY_Posts = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'nopaging' => true,
);
$the_query = new WP_Query( $MY_Posts );

Wordpress display custom post type with custom taxonomy and current term

Difficult this one:
I'm trying to all posts from a custom post type 'specialisaties' with the custom taxonomy 'specialismen' and need to load the current loaded 'term' from the URL.
Currently I have this code that outputs the term 'superpower' .
<?php $loop = new WP_Query(array('post_type'=>'specialisaties', 'taxonomy'=>'specialismen', 'term' => 'superpower', 'orderby' => 'menu_order', 'order' => 'asc')); ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php endwhile; ?>
This loads the specific post with the term 'superpower'. How do I fetch the 'term' dynamically from the URL I'm loading?
Thanks in advance,
Fixed it with get_term_by .
<?php $term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') ); ?>
<?php $loop = new WP_Query(array('post_type'=>'specialisaties', 'taxonomy'=>'specialismen', 'term' => $term->name, 'orderby' => 'menu_order', 'order' => 'asc')); ?>

Wordpress Costum Post Type and pagination would not cooperate

I have made my own costum post type in wordpress. I have already use WordPress's post type on my website. So i actually have two post type who running the loop on my website. But now i have kinda trouble with the pagination. It won't paginate from my own post type. Its like my post type and pagination wont work together. Because the pagination display that there are 2 pages, but when i click on page 2 it only display the same posts as on page 1.
<?php
$holdene = get_field('kampreferaterne');
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$the_query = query_posts(array(
'post_type'=>'kampreferater',
'paged'=>$paged
));
$kampf_args = array(
'post_type' => 'kampreferater',
'tax_query' => array( array(
'taxonomy' => 'hold_kategori',
'terms' => $holdene
))
);
$kampf = new WP_Query($kampf_args);
?>
<?php if ( $kampf->have_posts() ) : while ( $kampf->have_posts() ) : $kampf->the_post(); ?>
some content..
<?php wp_reset_postdata(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<nav class="pagination">
<?php wp_pagenavi(); ?>
</nav>
$holdene = get_field('kampreferaterne');
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$kampf_args = array(
'post_type' => 'kampreferater',
'paged' => $paged,
'tax_query' => array( array(
'taxonomy' => 'hold_kategori',
'terms' => $holdene
))
);
$kampf = new WP_Query($kampf_args);
Instead of wp_pagenavi(), use wp_pagenavi('query'=>$kampf);
Hope this helps.

WordPress not showing more than 10 posts

I am showing posts by shortcode into posts/pages of WordPress, and want to show an infinite list of posts, but it is showing only 10 posts.
Here is my code; please guide me what is wrong with my query.
$args = array( 'post_type' => 'post', 'cat' => '2', 'meta_key' => 'issue_of_article', 'meta_value' => $issue, 'posts_per_page' => -1, 'orderby' => 'artcle_category', 'order' => 'ASC');
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$loop->the_post();
<h3><?php the_title(); ?></h3>
endwhile;
}
Just add this in your argument
'posts_per_page' => -1
then you are done.
One more thing: you can change the default setting from admin to something other than 10. Go to the WordPress admin - Settings - Reading. There is an option like "Blog pages show at most". Type there the number of posts you want as the default.
Go to settings menu in the admin page
Settings -> Reading
Change the value for Blog pages show at most.
It will work.
Stupid question maybe but why do you call $loop->the_post(); twice ? Isn't it the source of the problem ? (each loop calls 2 posts at a time)
You can decide how many posts to show in the loop:
<?php wp_reset_query(); ?>
<?php
$loop = new WP_Query(
array(
'post_type' => 'resource',
'order_by' => 'post_id',
'order' => 'ASC',
'post_status' => 'publish',
'posts_per_page' => 100
)
);
?>
<?php while ($loop -> have_posts()): $loop -> the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
'posts_per_page' => -1
or
'posts_per_page' => 1000
Both of these should work.
At first - post WP questions at Wordpress.Stackexchange.com
The good way is to add in functions.php:
add_action('pre_get_posts','myfunc');
function myfunc($query){
if ($query->is_main_query() && $query->is_archive){
$query->set( 'posts_per_page', 1000);
}
return $query;
}

Wordpress query pages with certain template

Is there a way to query Wordpress pages that have certain template?
Here's what I got, but doesn't show anything:
<?php $my_query = new WP_Query(array( 'meta_key' => '_wp_page_template', 'meta_value' => 'template-city.php' )); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
Of course, there is page template file named template-city.php
if post_type is left out WP will look for post, and you are looking for pages.
<?php
$args = array(
'post_type' => 'page',//it is a Page right?
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-city.php', // template name as stored in the dB
)
)
);
$my_query = new WP_Query($args)
?>

Resources