Chop Chop - 3 months ago 15
MySQL Question

Override Wordpress core function _wp_mysql_week()

I would like to override the

_wp_mysql_week()
wordpress function located in
wp-includes/functions.php
since the actual
_wp_mysql_week()
is not complying with the ISO 8601 and asking the mode 1 to the
WEEK()
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.

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

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 )

to

WEEK( something, 1 )

to get your ISO 8601:1988 behavior. This explains query filtering. http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses

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

WEEK(#something,#1#)

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

WEEK(#DATE_SUB(#something,#INTERVAL#3#DAY#),#0#)

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. https://core.trac.wordpress.org/ The WordPress team is generally receptive to such requests.