最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Loop and Page template : my WP_query don't take args

programmeradmin0浏览0评论

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.

Share Improve this question edited Jul 23, 2016 at 9:36 Marcus asked Jul 23, 2016 at 6:48 MarcusMarcus 116 bronze badges 3
  • 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
Add a comment  | 

2 Answers 2

Reset to default 1

What 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.

发布评论

评论列表(0)

  1. 暂无评论