Skip Navigation

[Resolved] Admin Post type filter

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by TomW5440 2 years ago.

Assisted by: Minesh.

Author
Posts
#2349465

Tell us what you are trying to do?

I have two CPT Members (parent) > Comments (child) with a one to many relationship.

For comments in admin I have added an authors filter which is working fine to filter the comment authors. But now I need to add another filter for the member that it relates to. I've tried using the following code but it's not actually filtering the results -

//defining the filter that will be used so we can select posts by 'member'
function add_member_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 'members' or above
        $user_args = array(
            'show_option_all'   => 'All Members',
            'orderby'           => 'display_name',
            'order'             => 'ASC',
            'name'              => 'member_admin_filter',
            'who'               => 'premium',
            'include_selected'  => true
        );

        //determine if we have selected a user to be filtered by already
        if(isset($_GET['member_admin_filter'])){
            //set the selected value to the value of the member
            $user_args['selected'] = (int)sanitize_text_field($_GET['member_admin_filter']);
        }

        //display the users as a drop down
        wp_dropdown_users($user_args);
    }

}
add_action('restrict_manage_posts','add_member_filter_to_posts_administration');

//restrict the posts by an additional member filter
function add_member_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['member_admin_filter'])){

            //set the query variable for 'member' to the desired value
            $member_id = sanitize_text_field($_GET['member_admin_filter']);

            //if the member is not 0 (meaning all)
            if($member_id != 0){
                $query->query_vars['member'] = $member_id;
            }

        }
    }
}

add_action('pre_get_posts','add_member_filter_to_posts_query');

If you could please help me to get this working that would be ace! Thank you!

#2349509

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Basically it will require a custom code and to support such custom code would be beyond the support policy but still I will try to help you with your task.

Can you please share admin access and let me check your current setup. Please let me know where you added the custom code you shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2349533

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'll also require FTP access details in order to edit the code you added to functions.php file. Can you please send me FTP access details.

I have set the next reply to private which means only you and I have access to it.

#2349653

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To get the related child posts based on the parent ID, we use the Toolset post relationship API function: toolset_get_related_posts

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I've adjusted the code added to the functions.php file as given under:

function add_member_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['member_admin_filter'])){

            //set the query variable for 'member' to the desired value
            $member_id = sanitize_text_field($_GET['member_admin_filter']);

            //if the author is not 0 (meaning all)
            if($member_id != 0){
				
				 global $wpdb;
    
	// get member Id based on author ID
    $result = $wpdb->get_col( 
        $wpdb->prepare( "
            SELECT ID FROM {$wpdb->posts} pm
            WHERE pm.post_author  = '%s'
            AND pm.post_status IN ('publish', 'draft')", 
            $member_id
        ) 
    );
				
				// get related posts based on member ID
				$found_posts = toolset_get_related_posts(
											$result[0],
											'member-comment',
											'parent',
											1000000,
											0,
											array(),
											'post_id',
											'child'
										);
				if(!empty($found_posts)){
					$query->set('post__in', $found_posts);
				}else{
					$query->set('post__in', array(0));
				}
                
            }

        }
    }
}

add_action('pre_get_posts','add_member_filter_to_posts_query');

Can you please confirm it works as expected.

#2350391

My issue is resolved now. Thank you!

#2350549

Hello
I'm just testing this out and it's not quite working. If lists all users and most of these don't have a related parent member post. If you select a user who doesn't have a member post type it triggers a fatal error:

Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /home/alwaysre/public_html/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php:246 Stack trace: #0 /home/alwaysre/public_html/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php(177): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'parent') #1 /home/alwaysre/public_html/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/public_api/m2m.php(110): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(NULL, 'member-comment', Array) #2 /home/alwaysre/public_html/wp-content/themes/kadence-child/functions.php(180): toolset_get_related_posts(NULL, 'member-comment', 'parent', 1000000, 0, Array, 'post_id', 'child') #3 /home/alwaysre/public_html/wp-includes/class-wp-hook.php(307): add_member_filter_to_posts_query(Object in /home/alwaysre/public_html/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 246

So basically I need it to only list users that have a member post type. Or as a work around list only users with role as premium (premium role is for users who have member posts).

#2350553

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

With the following code added to the functions.php file:

//defining the filter that will be used so we can select posts by 'member'
function add_member_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 Members',
            'orderby'           => 'display_name',
            'order'             => 'ASC',
            'name'              => 'member_admin_filter',
            'role__in'               => 'premium',
            'include_selected'  => true
        );

        //determine if we have selected a user to be filtered by already
        if(isset($_GET['member_admin_filter'])){
            //set the selected value to the value of the author
            $user_args['selected'] = (int)sanitize_text_field($_GET['member_admin_filter']);
        }

        //display the users as a drop down
        wp_dropdown_users($user_args);
    }

}
add_action('restrict_manage_posts','add_member_filter_to_posts_administration');

'
I've changed the following line of code from:

  'who'               => 'premium',

To:

  'role__in'               => 'premium',

I can see it does displays only premium users with dropdown. Can you please confirm.

#2350573

Yes that's it. It only now displays premium users thank you.
It's still not 100% as those users who are premium role but have not yet created their member post appear and trigger the fatal error but I guess I can live with that.
Thank you.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.