1

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? – MinhTri Jul 23 '16 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 Jul 23 '16 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 Jul 23 '16 at 10:57
0

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();
  • Thanks but it's still not working. The above code displays my 4 posts and not the only 2 I want... I don't understand the mistake. – Marcus Jul 23 '16 at 7:49
  • have_posts has a side effect of advancing the loop. You need to wrap your while in an if (have_posts()) – Andy Macaulay-Brook Jul 23 '16 at 8:26
  • I have still the same problem... – Marcus Jul 23 '16 at 8:51
  • 1
    To expand on the WP coding standard comment, <?= get_the_title() ?> is a perfectly legitimate shorthand for <?php echo( get_the_title() ); ?> for any PHP version 5.4 and up. The WordPress coding standard's fear of the shorthand echo stems from WordPress officially supporting PHP 5.2 environments - despite the branch having crossed the end-of-life threshold 5.5 years ago. Generally, avoid the <? ?> shorthand because "XML complications" - but if you're only interested in supporting modern code that operates on an actual supported branch of PHP, baby don't fear the shorthand echo. – bosco Jul 23 '16 at 9:03
  • @bosco How can you explain my problem? – Marcus Jul 23 '16 at 9:38
0

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.

  • Thanks, the problem should come from another side of my site. I have unset all my plugins and I have clean my functions.php file, but the problem is still here. It is like my wordpress il clean, but it still don't work... I don't now in which side I have to search... – Marcus Jul 23 '16 at 16:06
  • Is there anything else in this template file or in your header.php? – Andy Macaulay-Brook Jul 23 '16 at 16:08
  • My template only contains the code you put in your answer. In my header.php file, I only use wp_nav_menu(), site_url(), home_url(), the_search_query(), bloginfo() and wp_head() methods. Can they be the source of the problem? – Marcus Jul 23 '16 at 16:11
  • They should be OK. So if your functions.php is empty and all the plugins are off, there shouldn't be anywhere else that code could hook in and interfere with your query. Is your theme a child theme? – Andy Macaulay-Brook Jul 23 '16 at 16:17
  • I made the theme, I don't think I made a mistake by creating a child theme instead of a normal one. How can I checked if this is the problem? In my style.css, I have put this parameters: Theme Name, Theme URI, Version, Description and Author. – Marcus Jul 23 '16 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.