I have a problem with a loop that I try to put into a custom Page template. I would like to display posts with conditions, but whatever I put in my WP_query parameters, it is not taking in consideration.
Here is all my template code:
<?php
/*
Template Name: Trends
*/
get_header(); ?>
<section id="top">
<?php the_post(); ?>
<div class="intro"><?= get_the_title(); ?></div>
<?php the_content();?>
<?php
$args = array(
'posts_per_page' => 2
);
$latest = new WP_Query( $args ); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
This code gives me all my posts, and not only the 2 I asked on args. But whatever I put it always gives me the default loop...
I've tried everything and I still don't understand.
Thanks for your help!
** EDIT **
This code works:
$query = new WP_Query(array(
'p' => 42
));
But this one don't:
$query = new WP_Query(array(
'post__in' => $post_ids,
'post_status' => 'publish',
'ignore_sticky_posts' => false,
'orderby' => 'post__in',
'posts_per_page' => 2
));
Whatever I put in my array, it doesn't works even if it is a p
parameter.
The second $query
displays all my posts.
I have a problem with a loop that I try to put into a custom Page template. I would like to display posts with conditions, but whatever I put in my WP_query parameters, it is not taking in consideration.
Here is all my template code:
<?php
/*
Template Name: Trends
*/
get_header(); ?>
<section id="top">
<?php the_post(); ?>
<div class="intro"><?= get_the_title(); ?></div>
<?php the_content();?>
<?php
$args = array(
'posts_per_page' => 2
);
$latest = new WP_Query( $args ); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
This code gives me all my posts, and not only the 2 I asked on args. But whatever I put it always gives me the default loop...
I've tried everything and I still don't understand.
Thanks for your help!
** EDIT **
This code works:
$query = new WP_Query(array(
'p' => 42
));
But this one don't:
$query = new WP_Query(array(
'post__in' => $post_ids,
'post_status' => 'publish',
'ignore_sticky_posts' => false,
'orderby' => 'post__in',
'posts_per_page' => 2
));
Whatever I put in my array, it doesn't works even if it is a p
parameter.
The second $query
displays all my posts.
- How many posts did you set to be displayed on Settings > Reading screen? – James Vu Commented Jul 23, 2016 at 9:25
- It is set to 10, but even if I change this value, I always have all my posts on the page. – Marcus Commented Jul 23, 2016 at 9:28
- @Dan9 I think that setting only affects the main loop and I'd expect the parameters you set in a custom query, or by filtering the main query, to have precedence. – Andy Macaulay-Brook Commented Jul 23, 2016 at 10:57
2 Answers
Reset to default 1What I see is , you are not following WP coding standard.
<?= get_the_title(); ?>
is not allowed in WP coding standard.
Now you are getting only the post and not as per your args provided, this means you have not reset your main WP Query.
We can write the above code as :
<?php
/*
Template Name: Trends
*/
get_header(); ?>
// Primary loop
<?php while( have_posts() ) : the_post(); ?>
<section id="top">
<div class="intro">
<? the_title(); ?>
</div>
<?php the_content();?>
<?php endwhile; ?>
<?php
// Paramter for getting 2 posts
$args = array(
'posts_per_page' => 2,
);
$latest = new WP_Query( $args ); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</section>
<?php get_footer();
As our attempts to help don't seem to have worked, I've taken your code as the question currently shows:
<?php
/*
Template Name: Trends
*/
get_header(); ?>
<section id="top">
<?php the_post(); ?>
<div class="intro"><?= get_the_title(); ?></div>
<?php the_content();?>
<?php
$args = array(
'posts_per_page' => 2
);
$latest = new WP_Query( $args ); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
I put that template into a fresh install of WP and assigned the template to a page. I made four posts name Post 1, Post 2, Post 3 and Post 4.
When I view the page I see Post 4 and Post 3 listed in that order. 2 of them.
So the answer to your question as it stands is that your code is working.