WordPress not showing more than 10 posts - wordpress

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;
}

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 );

In posts page show only last 2 years posts in Wordpress

I have created a page for posts called Blog, in Reading settings I have selected Blog page as posts page. Now what I want is I want show only last two years posts, not all. How can I achieve that?
Thanks in advance.
Here is the basic code for your query.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
'date_query' => array(
array(
'column' => 'post_date_gmt',
'before' => '2 year ago',
),
),
);
$query = new WP_Query( $args );
if($query->have_posts()): while($query->have_posts()): $query->the_post();
/*
* Your HTML styles to display the post
*/
?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
Also, please refer to the following document for more knowledge about it.
Query in WP Date
Hope its works for you
Thanks

wp-pagenavi not working on custom query 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.

permalink for multiple category

hi i had 15 pages for 2 categories.. example category A and category B.... while doing so i have displayed title and content... but permalink is creating for category A not for category B... how can i code for to get a permalink for second category...
$cat = get_the_category();
$id= $cat[0]->term_id;
$id1= $cat[1]->term_id;
$args = array('posts_per_page' => 15,
'cat'=> $id1,
'orderby'=> 'post_date',
'order'=> 'ASC',
'paged' => $paged,
'post_type'=> 'post',
'post_status' => 'publish' );
query_posts( $args );
if ( have_posts() ) while ( have_posts() ) : the_post();
$i++;
?>
<li><?php the_title();?></li>
<?php
endwhile;
wp_reset_query();
?>
but the permalink is creating for $id...
if you are using template or page to display this two categories post. You have to run two loop for every category. If you are trying to use shortcode or something. it will be different.
$cat = get_the_category();
$cat_01= $cat[0]->term_id;
$cat_02= $cat[1]->term_id;
$args_for_cat_01 = array('posts_per_page' => 15,
'cat'=> $cat_01,
'orderby'=> 'post_date',
'order'=> 'ASC',
'paged' => $paged, //I don't know what is this $paged??
'post_type'=> 'post',
'post_status' => 'publish');
$post_from_cat_01 = get_posts( $args_for_cat_01 );
foreach ( $post_from_cat_01 as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
//Do whatever you want here. Use divs or anything.
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
As above like this. Create another args for your second loop.
$args_for_cat_02 = array('posts_per_page' => 15,... change the cat => cat_02 ..ect
Then use the loop as above.
$post_from_cat_02 = get_posts( $args_for_cat_02 );
foreach ( $post_from_cat_02 as $post ) : setup_postdata( $post ); ?>
Remember to change the variables. I just use for more explanation.
If you need my help. Please find me any of social network by searching yeshansachithak.

Wordpress pagination always returns same 3 posts

So I have created dedicated page template, and on this page I want to list the 3 most recent blog posts, with the usual pagination links to take users to the previous or next 3 posts.
I have the list populated, and the links are appearing, but when I click on either the previous or next links I just get the same 3 posts as before. I can see the URL changes (/blog/page/2) but the posts being shown are always the three most recent ones.
UPDATE. After getting very frustrated I decided to take things back to basics. The following snippet is the only code I had in my template, just to isolate the loop basically, and this STILL didn't fix it. All I get is a singple post on the page, BUT if I manually type /page/2 at the end of the url it takes me to page 2 with a different post showing. However, the only link I see to do with pagination is 'Newer Posts' (and that only appears on page 2). How come 'older' posts isn't showing up?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'paged' => $paged,
);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
<article>
<h2 class="fnt25 noBtmMarg"><?php the_title(); ?></h2>
<div class="meta fnt18 pMarginBottom">Posted on: <?php the_date(); ?></div>
<div class="fnt22"><?php print_custom_field('listingDesc'); ?></div>
Read more...
<div class="clearfix"></div>
</article>
<?php endforeach; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Okay. After doing a little bit of research myself, it seems that the query_posts() is not the best for this type of scenario. Use get_posts(), read up on it a bit as well.
A good example (EDIT: better description):
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'paged' => $paged,
);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
?>
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'post',
'paged' => $paged,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div class="nav-previous alignleft">'.get_next_posts_link('Older posts', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div class="nav-next alignright">'.get_previous_posts_link('Newer posts', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Would you please check above code?

Resources