1

I am collecting values from a query string as an array. Then collecting those values and placing them into the value of the 'meta_query'.

I am having trouble where nothing gets outputted from the WP_Query. I feel like there is something wrong with the second array in the 'meta_query'.

I have tried to change the compare to 'LIKE', and that shows all the content instead of what is in the query. I also tried to remove the relation OR it has no effect on the result.

When I print out the variable, I achieve the correct array I want.

Query string:

?variable[]=value1&variable[]=value2

PHP code:

<?php
$variable_selected = $_GET['variable'];

$filter = array(
    'post_type' => 'my_custom_post_type',
    'meta_query' => array(
        'relation' => 'OR',
         array(
            'key' => 'my_post_field',
            'value' => $variable_selected,
            'compare' => 'IN'
            )
        )

    );

    $posts = new WP_Query( $filter  );

Any help on this will be great.

1 Answer 1

1

This $_GET['variable'] is an array, as per your URL query string ?variable[]=value1&variable[]=value2, $_GET['variable'][0] and $_GET['variable'][1] should return those 2 key values

Edit - after the discussion - making it dynamic

$meta_query = array();
if ( ! empty( $_GET["variable"] ) ) {
    if ( is_array( $_GET["variable"] ) ) {
        $meta_query['relation'] = 'OR';
        foreach ( $_GET["variable"] as $value ) {
            $meta_query[] = array(
                'key' => 'my_post_field',
                'value' => sanitize_text_field( (string) $value ),
                'compare' => '='
            );
        }
    } else {
        $meta_query = array(
            'key' => 'my_post_field',
            'value' => sanitize_text_field( (string) $_GET["variable"] ),
            'compare' => '='
        );
    }
}
$filter = array(
    'post_type' => 'my_custom_post_type',
    'meta_query' => array(
        'relation' => 'OR',
        $meta_query
    )
);
$posts = new WP_Query( $filter );

I have changed the operator to = because I was testing it and it worked on my local installation.

meta_compare (string) - Operator to test the 'meta_value'. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'NOT EXISTS', 'REGEXP', 'NOT REGEXP' or 'RLIKE'. Default value is '='. — codex

7
  • Thank you Samuel for your quick respond. Unfortunately this did not work for me. You are correct, when I place in $variable_selected[0] for the value it is first value in my array. The problem I feel is with the compare field? When I change it to LIKE it display content, all of the content tho?
    – Mike Boory
    Jun 6, 2016 at 11:53
  • Also I was hoping for something more dynamic, because the query string from my URL will change. It will not always have the same amount of values.
    – Mike Boory
    Jun 6, 2016 at 11:54
  • I understand, let me edit the answer.
    – Ismail
    Jun 6, 2016 at 11:55
  • As for the compare, you can run a test on PHPMYADMIN (db > table > search tab) to see exactly which operator you need (see here) Maybe = for full match or so..
    – Ismail
    Jun 6, 2016 at 11:59
  • @MikeBoory I have edited the answer.
    – Ismail
    Jun 6, 2016 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.