0
votes

The wp_query below does not work when using the post_parent operator.

If I remove this option the query runs but when I add back in, it does not.

I have identified the post parent id from the category URL in the admin and it's definitely correct with 24 posts in that category.

the url for the category is wp-admin/term.php?taxonomy=category&tag_ID=2893&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

<?php
// WP_Query arguments
$args = array(
    'post_parent'            => '2893',
    'post_type'              => 'post',
    //'post_status'            => array( 'publish' ),
    //'nopaging'               => true,
    // 'order'                  => 'ASC',
    // 'orderby'                => 'title'
);
// The Query
$sizes = new WP_Query( $args );
// The Loop
if ( $sizes->have_posts() ) {
    while ( $sizes->have_posts() ) {
        $sizes->the_title();
    }
} else {
    echo 'nothing here...';
}
// Restore original Post Data
wp_reset_postdata(); ?>  
2
What are you trying to achieve? Do you want to list pages that are tagged with the category term with id 2893? Then you will have to use 'category__in' => array() instead. post_parent is for getting pages ( or CPTs ) where posts have an actual parent - ninja
you are correct! thank you. though I'm now having the issue of the script timing out after 5 minutes, is there anything obvious that could cause this to happen? the DB is only 200mb or so - blackhill24
the script above is timing out after five minutes? Unless you have tens of thousands of posts the query should not even be close to take that long to run. Either you are running some more intense code in the loop-block, or something else on the template is causing issues. - ninja
okay thanks ill see if i can isolate the problem, it seems to be getting stuck on the if have posts statement - blackhill24
ninja, could you add your comment as an answer? I'll mark it solved for you as this was the issue for the point mentioned... thanks again - blackhill24

2 Answers

1
votes

Pass the argument as an integer, not a string ;)

<?php
// WP_Query arguments.
$args = array(
    'post_parent' => 2893, // This should be integer.
    'post_type'   => 'post',
    // 'post_status' => array( 'publish' ),
    // 'nopaging'    => true,
    // 'order'       => 'ASC',
    // 'orderby'     => 'title',
);

// The Query.
$sizes = new WP_Query( $args );
// The Loop.
if ( $sizes->have_posts() ) {
    while ( $sizes->have_posts() ) {
        $sizes->the_title();
    }
} else {
    echo 'nothing here...';
}
// Restore original Post Data.
wp_reset_postdata();
1
votes

If you want to list pages that are tagged with a category term (by id) you will have to use 'category__in' => array() instead. The post_parent argument is for getting pages ( or CPTs that are hierarchical ) where the ID passed is the page/post which is set as parent to other pages.

Example usage of getting posts tagged with category id:

$args = array(
    'category__in' => array($cat_id_1, $cat_id_2) // Where $cat_id_x is an integer of the category ID (2893 in your case).
);