Как в wordpress вывести записи?

Хочу вывести записи в wordpress, используя данную конструкцию:

        <div class="primary">
            <?php if (have_posts()) {while (have_posts( )) {the_post(); ?>
                <article>
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                </article>
            <?php }
            } ?>
        </div>

Но вместо записей выводятся страницы. Как мне исправить это?


Ответы (1 шт):

Автор решения: Valerii Vasiliev

Используйте для вывода класс WP_Query

        <div class="primary">

            <?php $args = []; ?>
      
            <?php $args['post_status'] = 'publish'; ?>
                
            <?php $args['post_type'] = 'post'; ?>
                        
            <?php $args['orderby'] = 'date'; ?>

            <?php $args['order']  = 'DESC'; ?>
                
            <?php $the_query = new \WP_Query($args);  ?>
 
            <?php if ($the_query->have_posts()) {while ($the_query->have_posts()) {$the_query->the_post();  ?>
                <article>
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                </article>
            <?php }
            } ?>
            
            <?php wp_reset_postdata(); ?>
        </div>

Вывод записей определяет параметр $args['post_type'] = 'post';

Подробнее тут https://wp-kama.ru/function/wp_query

→ Ссылка