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

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?

  • @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
    $catquery = new WP_Query( array( 'post_type' => 'post', 'category_name'=>'hello','posts_per_page' => 5 ) );
    while($catquery->have_posts()) : $catquery->the_post();
        <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php the_content(); ?>

  • 1
    he is interested in not having to have two queries. Mar 6 '18 at 9:41
  • Mark Kaplun please see again in my code I have only use one query 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 Mar 6 '18 at 11:44

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