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
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: