Chop Chop - 3 months ago 15
MySQL Question

Override Wordpress core function _wp_mysql_week()

I would like to override the

wordpress function located in
since the actual
is not complying with the ISO 8601 and asking the mode 1 to the
MYSQL function instead of the mode 3.

I'd like to put a filter or something in the theme functions.php but couldn't find how.

Thx for any help

Answer Source

The way this function is rigged up in WP 4.1, if you specify "Monday" as the first day of the week in the options table, you'll get ISO8601:1988 week numbers (MySQL Mode 1). Otherwise you'll get mode 0.

You can also intervene with a filter after the query is constructed but before it's passed over to MySQL. In your filter you can muck around with the text of the query to change

WEEK( something, 0 )


WEEK( something, 1 )

to get your ISO 8601:1988 behavior. This explains query filtering.

Notice that the _wp_mysql_week function helpfully generates its output with spaces where I have shown pound signs here.


If your week starts on some day besides Sunday or Monday, the generated query code looks like this:


So, you can probably use a regex in your filter to search for

WEEK( .+? 0 )

then change the digit to 1. But of course regexs have hazards.

You could also request that a new filter be added specifically to the output of _wp_mysql_week in order to allow user code to control it. The WordPress team is generally receptive to such requests.