Tell us what you are trying to do?
Hi there,
So I'm looking to add member username and member name so they are can be searched in wp admin when viewing a custom post type. The idea is that it's easy for admin to find all comments relating to a member.
I have two CPT Members > Comments
Member(parent) and Comments(child)
The Comments CPT only includes custom fields name(commentor name) and comment but it's setup in a way on the front end that when a comment is made it's linked to the member.
I'm not sure how to implement this so that member names can be searched in my CPT comments wp admin?
Thank you
Hi,
Thank you for contacting us and I'd be happy to assist.
I'm afraid, there is no built-in feature available to include custom search fields in the WordPress admin post list screen, so this will require code customization.
Here are some useful guides on the topic:
hidden link
hidden link
hidden link
For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
I managed to add User filter to the admin CPT. Here's how -
//defining the filter that will be used so we can select posts by 'author'
function add_author_filter_to_posts_administration(){
//execute only on the 'post' content type
global $post_type;
if($post_type == 'comment'){
//get a listing of all users that are 'author' or above
$user_args = array(
'show_option_all' => 'All Users',
'orderby' => 'display_name',
'order' => 'ASC',
'name' => 'author_admin_filter',
'who' => 'basic',
'include_selected' => true
);
//determine if we have selected a user to be filtered by already
if(isset($_GET['author_admin_filter'])){
//set the selected value to the value of the author
$user_args['selected'] = (int)sanitize_text_field($_GET['author_admin_filter']);
}
//display the users as a drop down
wp_dropdown_users($user_args);
}
}
add_action('restrict_manage_posts','add_author_filter_to_posts_administration');
//restrict the posts by an additional author filter
function add_author_filter_to_posts_query($query){
global $post_type, $pagenow;
//if we are currently on the edit screen of the post type listings
if($pagenow == 'edit.php' && $post_type == 'comment'){
if(isset($_GET['author_admin_filter'])){
//set the query variable for 'author' to the desired value
$author_id = sanitize_text_field($_GET['author_admin_filter']);
//if the author is not 0 (meaning all)
if($author_id != 0){
$query->query_vars['author'] = $author_id;
}
}
}
}
add_action('pre_get_posts','add_author_filter_to_posts_query');