Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)

  • Mauro
    Participant

    @maurodalu

    I’ve been testing this and found out that the code as is done in my previous post will break the members search feature from 3rd party plugins. So I modified this to use the “exclude” argument instead of the “include”.

    Since doing this is going to be a heavy call on the server, I’ve decided to store my exclusion list in the DB as an option. I’m sure this could be done with transients so feel free to improve changing the storage of the array in a transient.

    Note: shanebp mentioned this does not apply to ajax calls but it’s a filter to the members query so won’t it run whenever a call to the members is done no matter if it’s ajax or not if we exclude that piece of code?

    Revised code follows. I tested this and it works fine for me.

    /**
     * Only list active MemberPress members in the members directory.
     * sources:
     * https://buddydev.com/hiding-users-on-buddypress-based-site
     * https://buddypress.org/support/topic/member-loop-only-memberpress-members/
     * @param array $args args.
     *
     * @return array
     */
    function tmp_only_show_members_in_directory( $args ) {
        // do not exclude in admin.
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
            return $args;
        }
        $args['exclude'] = tmp_get_users_to_exclude_from_directory();
    
        return $args;
    }
    add_filter( 'bp_after_has_members_parse_args', 'tmp_only_show_members_in_directory' );
    
    /**
     * Returns an array of IDs to exclude, runs the queries if empty
     * @return false|mixed|void
     */
    function tmp_get_users_to_exclude_from_directory() {
        $ids_to_exclude = get_option('tmp_update_members_to_exclude_from_directory');
        if (empty($ids_to_exclude) || !is_array($ids_to_exclude) || count($ids_to_exclude) < 1) {
            tmp_update_members_to_exclude_from_directory();
        }
        return get_option('tmp_update_members_to_exclude_from_directory');
    }
    
    // Scheduled Action Hook
    function tmp_update_members_to_exclude_from_directory() {
        global $wpdb;
        $member_ids = $wpdb->get_col("SELECT DISTINCT user_id FROM ".$wpdb->prefix."mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00')");
        $user_ids = get_users(['fields'=>'ID']);
        $user_ids_to_exclude = array_diff($user_ids, $member_ids);
        return update_option('tmp_update_members_to_exclude_from_directory',$user_ids_to_exclude);
    }
    add_action( 'hook_name', 'tmp_update_members_to_exclude_from_directory' );
    
    // Schedule Cron Job Event
    function tmp_update_members_to_exclude_from_directory_job() {
        if ( ! wp_next_scheduled( 'tmp_update_members_to_exclude_from_directory' ) ) {
            wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'tmp_update_members_to_exclude_from_directory' );
        }
    }
    add_action( 'wp', 'tmp_update_members_to_exclude_from_directory_job' );

    Mauro
    Participant

    @maurodalu

    Please note the above solution won’t work if you have a custom table prefix (i.e. different than “wp_”).
    I’ve put together this function using the bp_after_has_members_parse_args filter. Posting it here in case anyone needs it. So far it looks like it works fine.

    /**
     * Only list active MemberPress members in the members directory.
     * sources:
     * https://buddydev.com/hiding-users-on-buddypress-based-site
     * https://buddypress.org/support/topic/member-loop-only-memberpress-members/
     * @param array $args args.
     *
     * @return array
     */
    function tmp_only_show_members_in_directory( $args ) {
        // do not exclude in admin.
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
            return $args;
        }
        global $wpdb;
        $member_ids = $wpdb->get_col("SELECT DISTINCT user_id FROM ".$wpdb->prefix."mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00')");
        $args['include'] = $member_ids;
    
        return $args;
    }
    add_filter( 'bp_after_has_members_parse_args', 'tmp_only_show_members_in_directory' );
Viewing 2 replies - 1 through 2 (of 2 total)
Skip to toolbar