Using WordPress ‘date_query_valid_columns’ PHP filter

The date_query_valid_columns WordPress PHP filter allows you to modify the list of valid date query columns.

Usage

add_filter('date_query_valid_columns', 'your_custom_function');
function your_custom_function($valid_columns) {
  // your custom code here
  return $valid_columns;
}

Parameters

  • $valid_columns (string[]): An array of valid date query columns. Defaults are ‘post_date’, ‘post_date_gmt’, ‘post_modified’, ‘post_modified_gmt’, ‘comment_date’, ‘comment_date_gmt’, ‘user_registered’, ‘registered’, ‘last_updated’.

More information

See WordPress Developer Resources: date_query_valid_columns

Examples

Add a custom date column

Add a custom date column named ‘my_custom_date’ to the list of valid date query columns.

add_filter('date_query_valid_columns', 'add_my_custom_date_column');
function add_my_custom_date_column($valid_columns) {
  $valid_columns[] = 'my_custom_date';
  return $valid_columns;
}

Remove a default date column

Remove ‘user_registered’ from the list of valid date query columns.

add_filter('date_query_valid_columns', 'remove_user_registered_column');
function remove_user_registered_column($valid_columns) {
  $key = array_search('user_registered', $valid_columns);
  if ($key !== false) {
    unset($valid_columns[$key]);
  }
  return $valid_columns;
}

Replace a default date column

Replace ‘post_date’ with ‘custom_post_date’ in the list of valid date query columns.

add_filter('date_query_valid_columns', 'replace_post_date_column');
function replace_post_date_column($valid_columns) {
  $key = array_search('post_date', $valid_columns);
  if ($key !== false) {
    $valid_columns[$key] = 'custom_post_date';
  }
  return $valid_columns;
}

Limit valid date columns

Limit the valid date query columns to only ‘post_date’ and ‘post_modified’.

add_filter('date_query_valid_columns', 'limit_valid_date_columns');
function limit_valid_date_columns($valid_columns) {
  return array('post_date', 'post_modified');
}

Clear valid date columns

Clear all valid date query columns.

add_filter('date_query_valid_columns', 'clear_valid_date_columns');
function clear_valid_date_columns($valid_columns) {
  return array();
}