skip to Main Content

i’m trying to get this code to work but it seems that the post__in condition is not being taken into account by the query:


$currentPostID = get_the_ID();

$parentPostID = wp_get_post_parent_id($currentPostID);
    
    
if ($parentPostID) {
    $args = array(
        'post_type' => 'post',
        'post_parent' => $parentPostID,
        'posts_per_page' => -1,
        'post__not_in' => array($currentPostID),
        'post__in' => $parentPostID,
        
    );

    $query = new WP_Query($args);

Is there a way to make it work?

I saw that it can be done by merging two queries but I’m looking for a way to do it in a single query to make them as fast as possible

2

Answers


  1. It expects an array of post IDs, but you are passing a single post ID. Try this:

    $currentPostID = get_the_ID();
    
    $parentPostID = wp_get_post_parent_id($currentPostID);
        
        
    if ($parentPostID) {
        $args = array(
            'post_type' => 'post',
            'post_parent' => $parentPostID,
            'posts_per_page' => -1,
            'post__not_in' => array($currentPostID),
            'post__in' => array($parentPostID),
            
        );
    
        $query = new WP_Query($args);
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search