I have a hierarchical custom post type which has posts with no children and also posts that have one or multiple direct children (only up to a depth of 1, i.e no children of children).
For example:
I’m using the index.php with a standard main loop of have_posts() & the_post() listing all posts – Parent 1, Parent 2, Child 1, Child 2 and is also used as the template for almost everything else such as taxonomy archives, search results etc.
I want to be able to use a query string “show_posts” in the URL to modify how the posts are being shown based on their parent/child relationship everywhere that the index.php template is used (so that search results and archives can be modified in the same way).
The query string “show_posts” could have the following values:
Which will result in:
This is my code so far:
The first two conditions for the parents & children pages work perfectly, but I’m stuck at finding a way, looking at the WP_Query arguments, to get posts that are parents without children.
As far as I can see, there’s no way to do that with only the functionality available in WP_Query and related functions, or am I wrong?
I’m currently looking into creating an array of IDs of childless parent posts from outside the query by using get_posts() and get_children([‘post_parent’ => $parent_id]) and then passing that array into the query, but I’m not sure if there could be a better option and a simple one liner like the parents-only and children-only conditions from within the query?
- Hiện tại chúng tôi có hơn 25 nghìn bài văn mẫu các thể loại.
- Kho tài liệu, đề thi học sinh giỏi các lớp vô cùng phong phú.
- Mỗi ngày cập nhật hơn 100 đề thi chất lượng từ các website bán tài liệu lớn.
Tải đề thi VIP với giá siêu rẻ tại Vip.Dethihsg247.Com