I’m currently trying to fetch all my pages from different post types and display them on a single page with a WP Query but I wanted to display them in such a template in the HTML instead of it all coming as a big list of course I’m not entirely sure if this is even possible
Parent
Child
Parent
Child
Parent
Parent
Parent
This is what I’m using so far
$args = array(
'post_type'=> 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$result = new WP_Query($args);
?>
<section>
<h2><?php echo $post_type; ?></h2>
<?php
while ($result->have_posts()) {
$result->the_post();
$post = get_post();
?>
<ul>
<li class="">
<a href=""><?php echo the_title(); ?></a>
</li>
</ul>
<?php
}
wp_reset_postdata();
?>
</section>
<?php }
I tried using wp_list_pages
but I’m not sure how to use that for different post types and also I would like to have limited posts per page not display everything
2
Answers
WP_Query
is not the right tool for the job!WordPress has special functions for what you’re looking for:
Well, that’s why docs are here for!
For example,
wp_list_pages
accept an argument calledpost_type
which lets you specify which "post type" you’re interested in, and its default value is the wordpress "page" type. If you want to change it to your custom post type then you could use something like this:Again, there is docs for that!
if you use
get_pages
for example, then there is an argument for limiting the number of pages shown on the page and that’s callednumber
. Thenumber
argument is an integer and represents the number of pages to return and its default is0
(all pages).Using
get_pages
also you could specify your custom post type to query. The argument for that is calledpost_type
and its default value is the wordpress "page" type.If you really need/want to use
WP_Qery
then you could do it like this:WP_Qery
+get_pages
Which will output this:
Fully tested and works on wordpress
5.8
.