Trouble querying term meta via REGEXP
-
So I am having trouble querying specific taxonomy terms based on regex values. Here is what I am trying to do.
I have a custom taxonomy called issue assigned a term meta labeled meta_issue_published_date. The meta is stored in the db in the format ‘yyymmdd’. Looking at the table in the db all of the issues and their published dates are being saved correctly.
Now on the frontend I am converting the dates to ‘Y’ format and allowing users to select a year. Here is an example of the function I am using:
/** * Get the year from date field */ function namespace_get_the_year( $issue ) { $date = get_term_meta( $issue->term_id, 'meta_issue_published_date', $issue ); $year = date( 'Y', strtotime( $date ) ); return $year; }
This is working fine. However, I am trying to run a query once a user selects a year and find all issues whose meta_issue_published_date begins with the year they selected. For instance, if a user selects ‘2015’, I want to query all issues whose meta_issue_published_date beings with 2015. Here is how I am trying to accomplish this:
// $date is determined by users choice and is returning the correct value // Get all issues belonging to the year $args = array( 'key' => 'meta_issue_published_date', 'value' => '^(' . $date . ')', 'compare' => 'REGEXP' ); $issues = get_terms( 'issue', $args );
However, this isn’t doing at all what I would like and I don’t know what exactly is going on. Running a test when selecting ‘2015’, here is what the meta_query arguments look like, followed by the meta_issue_published_date values of each returned issue:
Array ( [key] => meta_issue_published_date [value] => ^(2015) [compare] => REGEXP ) 20141202 20141201 20150601
There are other meta_issue_published_date values in the db other than the ones returned, so I’m not sure what exactly might be going on here. I’m not proficient in REGEX, so I am assuming I’m going something wrong for the value key in my arguments.
A little wisdom would be appreciated…
- The topic ‘Trouble querying term meta via REGEXP’ is closed to new replies.