kanbanpress_modify_query_args

Adding Custom Conditions to the KanbanPress Board Query

With the kanbanpress_modify_query_args hook, you can programmatically add custom conditions to the query that fetches posts for the KanbanPress board. This enables developers to filter posts based on specific criteria, such as posts authored by the current user or posts with specific metadata.

Hook Overview

The kanbanpress_modify_query_args filter allows you to modify the query arguments before the posts are fetched. It passes the current query arguments and the board index to your custom function.

Hook Definition:

phpCopy code$query_args = apply_filters('kanbanpress_modify_query_args', $query_args, $board_index);

Usage Example

  1. Filter Posts by Current User

To display only posts authored by the current logged-in user, add the following to your theme’s functions.php file or a custom plugin:

PHP
function filter_posts_by_current_user($query_args, $board_index) {
    if (is_user_logged_in()) {
        $current_user_id = get_current_user_id();
        $query_args['author'] = $current_user_id;
    }
    return $query_args;
}
add_filter('kanbanpress_modify_query_args', 'filter_posts_by_current_user', 10, 2);
  1. Filter Posts by Custom Meta Field

To display posts that have the current user’s ID in a custom meta field users_who_have_access, use the following code:


PHP
function filter_posts_by_user_meta($query_args, $board_index) {
    if (is_user_logged_in()) {
        $current_user_id = get_current_user_id();
        $meta_query = isset($query_args['meta_query']) ? $query_args['meta_query'] : [];
        $meta_query[] = [
            'key' => 'users_who_have_access',
            'value' => $current_user_id,
            'compare' => 'LIKE'
        ];
        $query_args['meta_query'] = $meta_query;
    }
    return $query_args;
}
add_filter('kanbanpress_modify_query_args', 'filter_posts_by_user_meta', 10, 2);

For filtering with the calendar add-on, see kanbanpress_calendar_query_args