1

This question already has an answer here:

I did create a custom page template and assigned to a page.

I have this code inside:

$news_filter = array( 
    'category'        => '53', 
    'posts_per_page'  => 5,
);

$news = get_posts( $news_filter );

echo count ( $news );

foreach ( $news as $post ) :  setup_postdata( $post ); 
   the_post_thumbnail();
   the_title();
   the_content();
   endforeach;

It works, but I would learn more and more wordpress so, I would ask, if is there is a way to avoid having to run two queries? For example, I know existing of (custom) filter. Instead to make the loop twice (by WP and by me), can we add a filter, block WP and perform a custom loop?

marked as duplicate by Max Yudin, Christine Cooper, Jack Johansson, EAMann Mar 13 '18 at 12:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @MaxYudin how the link you put help me? pre_get_posts blocks all wp site. I need only a page with "custom"query. – sineverba Mar 6 '18 at 14:13
  • You are free to write a custom query and, please, come back when it does not work. – Max Yudin Mar 6 '18 at 17:30
-2
<?php
    $catquery = new WP_Query( array( 'post_type' => 'post', 'category_name'=>'hello','posts_per_page' => 5 ) );
    while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
    <li>
        <h3>
        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    </h3>
</li>
<li>
    <?php the_content(); ?>
</li>

  • 1
    he is interested in not having to have two queries. – Mark Kaplun Mar 6 '18 at 9:41
  • Mark Kaplun please see again in my code I have only use one query – Dharmishtha Patel Mar 6 '18 at 10:10
  • that is two queries, because there is first the main one when the page is loaded, and than your query – Mark Kaplun Mar 6 '18 at 11:44

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