lavodnas
lavodnas

Reputation: 56

filter custom post type archive page using meta_query with multiple arrays using acf relationship field

As the title suggests I'm trying to filter a custom post type archive page using meta_query with multiple arrays using an acf relationship field.

This is what I have so far using the documentation and tutorials at acf. Both the keywords filter and the project_ref filter work independently if I use 'relation' => 'AND' but only keywords works using 'OR' and never together. Also, project_ref is a post id.

 // functions.php
   $meta_query = $query->get('meta_query');

        // allow the url to alter the query
        if( !empty($_GET['keywords']) OR !empty($_GET['project_ref']) )
        {
          $keywords = explode(',', $_GET['keywords']);
          $projects = $_GET['project_ref'];

          // Add our meta query to the original meta queries
          $meta_query[] = array(
            'relation' => 'OR',
            array(
              'key'   => 'keywords',
              'value'   => $keywords,
              'compare' => 'LIKE'
            ),
            array(
              'key' => 'project_ref',
              'value' => $projects,
              'compare' => 'LIKE'
            )
          );
        }

So if my url is website.com/customposttype/?keywords=one,two&project_ref=684 the posts filter by the keywords but not the project_ref post id. I also get an error Warning: trim() expects parameter 1 to be string, array given in .../wp-includes/meta.php on line 1400.

Any help would be greatly appreciated. I feel like I'm close but I definitely seem to be missing something.

Thanks in advance.

Upvotes: 2

Views: 1849

Answers (1)

Mark Skeet
Mark Skeet

Reputation: 81

It looks like you could be using the wrong compare value for your keywords portion of the query.

As per the WP_Query codex entry:

value (string|array) - Custom field value. It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'. You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.

As your error is indicating you're passing an array. In SQL when you want to check for the existance of a number of required values you would use the IN compare option.

So your code would be:

 // functions.php
   $meta_query = $query->get('meta_query');

        // allow the url to alter the query
        if( !empty($_GET['keywords']) OR !empty($_GET['project_ref']) )
        {
          $keywords = explode(',', $_GET['keywords']);
          $projects = $_GET['project_ref'];

          // Add our meta query to the original meta queries
          $meta_query[] = array(
            'relation' => 'OR',
            array(
              'key'   => 'keywords',
              'value'   => $keywords,
              'compare' => 'IN'
            ),
            array(
              'key' => 'project_ref',
              'value' => $projects,
              'compare' => 'LIKE'
            )
          );
        }

Upvotes: 1

Related Questions