This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

'post_parent' != 1

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #24489
    am12313am12313
    Participant

    I’d like exclude parents posts from the AJAX search results. Ie: ‘post_parent’ != 1

    How do I do that?

    Thank you.

    #24504
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    It is possible via a custom code, this knowledge base will help you out: https://wp-dreams.com/knowledge-base/excluding-posts-or-cpt-by-parent-ids/

    #24508
    am12313am12313
    Participant

    Hi! Thanks for your reply!

    I’m actually looking to exclude posts; however, this code does not appear to work:

    add_filter( ‘asp_query_args’, ‘asp_exclude_by_ids’, 10, 2 );

    function asp_exclude_by_ids( $args, $id ) {

    $args[‘post__not_in’] = array(539);
    return $args;
    }

    Thank you

    #24511
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    I thought you are looking for a way to exclude posts by parent IDs. You can simply exclude posts by IDs under the Advanced Options -> Exclude/Include results panel 🙂

    #24516
    am12313am12313
    Participant

    Hey, Thanks.

    I need to be able to do this programmatically. Can I use the above code?

    The reason is: I need to exclude all posts where post_parent == 0. In other words, I want only child posts to appear in the search.

    My solution was this:

    $args1 = [
    ‘post_type’ => ‘wines’,
    ‘post_parent’ => 0,
    ‘nopaging’ => true,
    ‘fields’ => ‘ids’
    ];
    $ids = get_posts( $args1 );
    add_filter( ‘asp_query_args’, ‘asp_exclude_by_ids’, 10, 2 );

    function asp_exclude_by_ids( $args, $id ) {

    $args[‘post__not_in’] = $ids;
    return $args;
    }

    However, that code does not appear to be compatible.

    Thank you.

    #24519
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    Oh okay, you can actually do that, but the argument name is different. Use this:

    $args['post_not_in'] = $ids;

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘'post_parent' != 1’ is closed to new replies.