You’re using a raw SQL query to do a post query, this is bad, because:
- There are already APIs that do this
- It is no longer inheriting the default parameters
- It doesn’t go through the caching system so it can be slower
Intead, use WP_Query
.
The reason you’re getting trashed posts is because you’re not specifying what post status you want. By default WP_Query
defaults to published only.
Here is your equivalent post loop:
$query = new WP_Query( array(
'post_type' => 'page',
'post_parent' => $post->ID,
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
) );
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// process each post
}
}
Related Posts:
- Advanced Custom Fields – Get custom fields from parent page
- Move from old custom field to new post_thumbnails
- Is there a hook / action that is triggered when adding or removing a post thumbnail?
- How can I migrate all of my custom field thumbnails to the built-in post featured image?
- automatically set “Featured Image” the same as the og:i that is set in a custom field
- Adding a custom field or metabox to the post-thumbnail widget?
- Add url from Custom Field as ‘Featured Image’. Code not working
- Display the contents of a custom field of a page on their child
- Advanced Custom Fields dynamic update_field
- For homepage images (for small business website), is it better to use custom fields or post_thumbnail?
- How to mass update custom fields for more than 20k posts
- how to make nsfw post with thumbanil
- Can I use a Custom Field as the Featured Image URL?
- How to display childrens custom fields?
- Getting the Featured image URL and inserting it as Custom Field on Post update / publish
- User customising position of WordPress Featured Image
- Set featured image from custom field URL
- Custom field as featured image caption
- Is there a way to dump all registered sidebar/widget?
- WP Insert post with post_thumbnail
- Custom field image to Featured Image
- Selectively hiding or allowing thumbnails of featured images on front page
- use custom field value as featured image
- Can you generate a featured image from two images from custom fields?
- Load one page with parent and child pages
- Use a different catalog image than the featured image
- Automatically set the_post_thumbnail to Custom Field Value
- Why is wp_list_pages altering $post->ID of the page?
- Display Child Page with custom fields within Parent Page
- Related post thumbnail not showing up
- Post thumbnail size for custom field only
- How to mark a image attachment as background image?
- Custom field for image, not showing image!
- Show/Hide Featured Image or replace it with custom field [closed]
- Crop custom image size vs actual size
- Displaying child pages and file URL in an ACF relationship field shortcode
- Advanced Custom Fields: how do I check to see if a value is set in an field? [closed]
- WP_Query – Order results by meta value
- Add filter menu to admin list of posts (of custom type) to filter posts by custom field values
- getting all values for a custom field key (cross-post)
- How to add a custom field in the advanced menu properties?
- Custom query with orderby meta_value of custom field
- How to filter post listing (in WP dashboard posts listing) using a custom field (search functionality)?
- Using meta query (‘meta_query’) with a search query (‘s’)
- Can I exclude a post by meta key using pre_get_posts function?
- Add validation and error handling when saving custom fields?
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Show Custom Fields in Quick Edit
- Where are custom field values stored in the database
- Validating Custom Meta Box Values & Required Fields
- Max length of meta_value
- Add custom fields to wp native gallery settings
- How to fix missing custom fields after upgrading to WordPress 4.8.1?
- How to enable custom fields for pages (if not a bad practice)?
- How can I add extra attribute in the ‘Page Attribute’ section in wp-admin for pages?
- Is there a way to set default custom fields when creating a post?
- Custom post meta field effect on the performance on the post
- How to get custom post meta using REST API
- Custom field/meta populated by dropdown of existing posts?
- Difference between meta keys with _ and without _ [duplicate]
- Is there any action filter/hook for validating a custom field before publishing the post?
- Remove Dimension from wp_get_attachment_image
- Orderby meta_value only returns posts that have existing meta_key
- Underscores in custom fields
- What is the index [0] for on post meta fields?
- What is “meta_input” parameter in wp_insert_post() used for?
- Query Posts or Get Posts by custom fields, possible?
- How to enable revisions for post meta data?
- Sortable Custom Columns in User Panel (users.php)?
- Any way to add custom options to Gallery Settings?
- The “_encloseme” Meta-Key Conundrum
- ORDER BY custom field value
- Individual Widgets per Page
- Add subtitle to Woocommerce product title
- Best way to programmatically remove a category/term from a post
- Importing data for advanced custom fields plugin?
- Gutenberg add a custom metabox to default blocks
- SQL Query to copy value of a meta_key into another meta_key
- ‘Preview Changes’ for custom meta boxes?
- Adding a custom field to the site identity menu
- Using TinyMce with textareas in meta boxes on custom post types
- Filter WP_Query for posts having a certain meta-value
- How to correctly call custom field dates into a posts_where filter using SQL statements
- Using get_post_meta with new_to_publish
- Is there a way to get protected meta fields through any of the available built-in WordPress APIs? (xmlrpc, wp-json)
- Add a Jquery Datepicker to custom field in post edit
- add meta box – custom field : which to choose?
- Preserving line breaks when saving and displaying custom fields data
- How to return Meta data from the REST API?
- Can I query custom meta data through WP_Query
- Add custom field to the archive page?
- Saving custom data for each user
- Is it safe to store a user setting you don’t want the user to ever modify as a user option?
- Tabindex on text input immediately after WordPress title input
- Including custom fields in search?
- Add custom fields to search
- SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
- Attaching Metadata to a Taxonomy Item
- Add metabox to document tab in gutenberg
- How to display all custom fields associated with a post?