How to change the initial spreadsheet filters for every author

The spreadsheet works like this by default:

  • When you open it for the first time, it shows all the rows. If you’re editing a post type and you’re an “author”, you view only your own posts.
  • The next time you open the spreadsheet, it shows the same filters that you used in the previous session. For example, if you searched posts with the “Car” keyword, it will show the Cars the next time so you can continue editing where you left off or remove the filters manually to start fresh.

In this tutorial we’ll show you how to change the initial filters programmatically.

This can be helpful for improving the users workflow. For example:

  • You can show the posts missing SEO title automatically when the SEO employee opens the spreadsheet.
  • You can show the products with stock < 10 when the store employee opens the spreadsheet

The goal is to show the rows that the user wants to view or edit automatically.

Step 1. You need to find the filters parameter

  • Open the spreadsheet
  • Make the search using the “Search” tool. For example, find the posts missing SEO title
  • Reload the spreadsheet page
  • Open the browser console in the developer tools, and execute the following code to see the search parameters
JSON.stringify(vgse_editor_settings.last_session_filters)

Example:

Step 2. Modify the active filters programmatically

The code is simple:

  • We use the vg_sheet_editor/js_data filter to modify the spreadsheet settings
  • We check if the current spreadsheet is the product sheet (this is the post type key for post types, user for users, and taxonomy key for the taxonomies spreadsheet)
  • We check if the current user = 20, we modify the last_session_filters to use the JSON with the search query we got from the step 1

This way you can make any search in step 1, copy the json, and use the json here. You can do advanced things, like changing filters for entire user roles, specific user ids, etc.

add_filter('vg_sheet_editor/js_data', 'wpse_custom_session_filters', 99, 2);

function wpse_custom_session_filters($all_settings, $current_provider_in_page) {

  if( $current_provider_in_page === 'product' ){
    if( get_current_user_id() === 30 ){
      $all_settings['last_session_filters'] = json_decode('{"meta_query":{"2":{"key":"_yoast_wpseo_title","source":"meta","compare":"=","value":""}}}', true);
    }
  }
  return $all_settings;
}
Close Menu