Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have followed this tutorial and this one to add a custom column to a custom post type edit screen, showing the post parent of each post. My question: How can I make the post parent clickable just like in the Category column of the standard manage posts screen so that when I click on a specific post parent, only posts from that category will be shown in the edit screen?

Following scribu's tutorial the column is populated with the title of the post parent:

function manage_mytype_columns( $column, $post_id ) {
global $post;
        $pp_id = get_post($post_id)->post_parent; /*The post parent id is stored also as post meta, so I could also use $pp_id = get_post_meta($post_id, 'post_parent', true) ); */
        if ( empty( $pp_id ) ) {
            echo __( 'No post parent' );}
        else {
            $pp_title = get_post($pp_id)->post_title;
                            echo $pp_title; }
}
add_action( 'manage_mytype_posts_custom_column', 'manage_mytype_columns', 10, 2 );

I tried to achieve what I want (following the tutorial from DevPress, but obviously without much understanding of what am I supposed to do):

function manage_mytype_columns( $column, $post_id ) {
global $post;
        $pp_id = get_post($post_id)->post_parent; /*The post parent id is stored also as post meta, so I could also use $pp_id = get_post_meta($post_id, 'post_parent', true) ); */
        if ( empty( $pp_id ) ) {
            echo __( 'No post parent' );}
        else {
            $pp_title = get_post($pp_id)->post_title;
                            $pp_url = add_query_arg(array('post_type' => $post->post_type, 'post_parent' => $pp_id), 'edit.php');
                           echo '<a href="'.$pp_url.'">'.$pp_title.'</a>'; }
}
add_action( 'manage_mytype_posts_custom_column', 'manage_mytype_columns', 10, 2 );

This makes the post parents clickable and once I click on one the url is something like "edit.php?post_type=mytype&post_parent=post_parent_ID" but it still shows all the posts, no matter what parent they have. I know I am missing something, probably some query hook (I read also something about a restrict posts hook), but I just don't have the proper knowledge to make this work...properly. :) Please help. Thx, Radu

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The problem is that 'post_parent' is not a public query var, i.e. you can use it only from code.

To be able to use it from the URL, you just need to add these lines of code:

function make_post_parent_public_qv() {
    if ( is_admin() )
        $GLOBALS['wp']->add_query_var( 'post_parent' );
}
add_action( 'init', 'make_post_parent_public_qv' );
share|improve this answer
    
Thank you! You are quite awesome! :) –  rereradu Sep 29 '11 at 12:35
1  
For anyone who's interested, the solution to filter posts by any meta key or post field is my second code followed by scribu's solution, all put together in the functions.php file in your theme folder. Thanks again scribu! –  rereradu Sep 29 '11 at 12:42
    
Thanks it was very helpful –  user1983017 Nov 2 '13 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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