2

How to get Custom Post ID by adding code to child theme's function. The following code works fine for the regular post, but can't figure out for the custom post types.

add_filter( 'manage_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_posts_custom_column', 'revealid_id_column_content', 5, 2 );

function revealid_add_id_column( $columns ) {
   $columns['revealid_id'] = 'ID';
   return $columns;
}

function revealid_id_column_content( $column, $id ) {
   if( 'revealid_id' == $column ) {
   echo $id;
 }
}
2
  • Have your read the manage_$post_type_posts_columns doc? Your custom post type is called custom ??
    – Laxmana
    Commented Feb 22, 2017 at 11:52
  • @Laxmana, can you please give me the code, I try to read it and didn't understand well. My custom post type is :- $estate_property. Thanx Commented Feb 22, 2017 at 15:27

2 Answers 2

1
add_action( 'manage_posts_custom_column', 'id_data' );
add_filter( 'manage_posts_columns', 'id_column'  );
function id_column( $defaults ) {
    $defaults['id'] = 'ID';
    return $defaults;
}
function id_data( $column_name ) {
    global $post;
    switch ( $column_name ) {
    case 'id':
        echo $post->ID;
    }
}
5
  • it works fine for the regular posts, but it is not showing/working on the custom post type Commented Feb 22, 2017 at 11:19
  • I attached image in previous comments,please check it .It's in custom posttype.Your code works fine in my custom posttype. Have you added any other condition?
    – Sonali
    Commented Feb 22, 2017 at 11:25
  • I used Reveal ID plugin and it works fine on my custom post types. But I tend use less plugins. Therefore I assume that there must be missing something. I am using WP residence theme. Commented Feb 22, 2017 at 11:38
  • Your code works absolutely fine in custom posttype, without using 'Reveal ID plugin' ,may be in theme or in any plugin there is condition that's why it is not working.
    – Sonali
    Commented Feb 22, 2017 at 12:04
  • I am trying to figure it out, and once I know the answer. I will let you know. thanks anyway Commented Feb 22, 2017 at 16:33
0

For custom post types there are the corresponding filters manage_{$post_type}_posts_columns and manage_{$post_type}_posts_custom_column where {$post_type} is your custom post name ($post_type variable in register_post_type).

I assume, as you said in comments, that your custom post type is called estate_property.

So:

add_filter( 'manage_estate_property_posts_columns', 'revealid_add_id_column', 5 );
add_action( 'manage_estate_property_posts_custom_column', 'revealid_id_column_content', 5, 2 );

function revealid_add_id_column( $columns ) {
   $columns['revealid_id'] = 'ID';
   return $columns;
}

function revealid_id_column_content( $column, $id ) {
   if( 'revealid_id' == $column ) {
   echo $id;
 }
}

This will work only for your custom post type. If you want to support multiple custom types read the doc of manage_posts_custom_column.

From WordPress Codex: Note that if the custom post type has 'hierarchical' => true, then the correct action hook to use is manage_pages_custom_column.

1
  • Thank you. I put the code in the function.php - but nothing is happening. Am I missing something? Maybe custom post type has 'hierarchical' => true. Can you plz tell me the next step Commented Feb 22, 2017 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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