0

enter image description here

I want a filter like this in my custom post "books". This filter should only bring up the parent post.

1 Answer 1

0

In addition to my previous answer add following code. It should work fine

add_filter( 'views_edit-books', 'add_filter_link' );
function add_filter_link( array $views ) {
    $url = add_query_arg( array('bfilter'=>'parents','post_parent'=>0,'post_type'=>'books'), 'edit.php' );
    $views[ 'post_parent' ] = sprintf(
        '<a href="%1$s"%2$s>%3$s</a>',
        esc_url( $url ),
        ( is_filter_active() ) ? ' class="current" aria-current="page"' : '',
        'Post Parents'
    );
    return $views;
}

function is_filter_active() {
    return (filter_input( INPUT_GET, 'bfilter' ) ==='parents');
}

if you already not used bellow code then add that too

function make_post_parent_public_qv() {
    global $pagenow;
    if ( is_admin() && $pagenow == 'edit.php' )
        $GLOBALS['wp']->add_query_var( 'post_parent' );
}
add_action( 'init', 'make_post_parent_public_qv' );
7
  • I add this code in restrict_manage_posts hook but I got nothing. Jun 18, 2020 at 10:40
  • Could you please demonstrate the full code. I'm very new at this. It would be a great help to me. Please Jun 18, 2020 at 10:51
  • you don't need to add this within anything. Just use this code alone. Jun 18, 2020 at 11:16
  • O my god.. You are GREAT. Thanks Thanks Thanks. Please suggest me where can I learn more about this kind of stuff. And one more thing, can I filter child posts the same way. Jun 18, 2020 at 11:51
  • Just explore wordpress codex a bit. hooker.io is another great place Jun 18, 2020 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.