In posts page show only last 2 years posts in Wordpress - 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

Related

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

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.

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 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?

Wordpress - List posts of a custom post type from sub-categories

I have a custom post type called my-plays, within which I've created the categories short-plays and long-plays. I would like to just be able to output a list of the posts in each category... at the moment I can only find code which just outputs a lists of all posts in my-plays. Is it possible to list posts in a category of a custom post type?
Taken from here.
<?php
$args = array
(
'numberposts' => 3,
'category' => array(48,43,49,46,47,44,51,50,42),
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish'
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

Resources