• Hi there!
    I am working on a website which contains a class which echoes data from the database to the frontend. The data is stored in the usermeta table. I have added an extra field to the usermeta table using the Advanced Custom Fields module. The query as it is now (without the extra field) is:

    if ($_POST['plaats'] != '') { $meta_query[] = array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE'); }

    (plaats being Dutch for city)

    The query should be something like SELECT * FROM usermeta WHERE $_POST[‘plaats’] LIKE city OR $_POST[‘plaats’] LIKE city2

    I got this far:

    if ($_POST['plaats'] != '') { $meta_query[] = array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
    $meta_query[] .= array(
    'relation' => 'OR',
    array(
    'key'     => 'city2',
    'value'   => $_POST['plaats'],
    'compare' => 'LIKE'
    )
    ); }

    But as you might guess I cannot get it to work.

    Any help would be greatly appreciated!
    Thanks!
    A WordPress newbie πŸ™‚

Viewing 15 replies - 1 through 15 (of 16 total)
  • I think this is what you want:

    if ($_POST['plaats'] != '') {
       $meta_query = array(
          'relation' => 'OR',
          array(
             'key' => 'city',
             'value' => $_POST['plaats'],
             'compare' => 'LIKE'
          ),
          array(
             'key'     => 'city2',
             'value'   => $_POST['plaats'],
             'compare' => 'LIKE'
          )
       );
    }
    Thread Starter duucmeister

    (@duucmeister)

    Hi there,
    Thanks very much for your response and help!
    When I entered the script however and tried to use the search function (frontend) all the entries in de database were echoed. Do you by any chance know if the script as you suggested it works like
    SELECT * FROM ... WHERE value1 = $var1 AND value2 = $var2 OR value3 = $var3
    or
    SELECT * FROM ... WHERE value1 = $var1 AND (value2 = $var2 OR value3 = $var3)
    Cheers!

    Did you put $meta_query into your $args array? Like this:

    if ($_POST['plaats'] != '') {
          $meta_query = array(
             'relation' => 'OR',
             array(
                'key' => 'city',
                'value' => $_POST['plaats'],
                'compare' => 'LIKE'
             ),
             array(
                'key'     => 'city2',
                'value'   => $_POST['plaats'],
                'compare' => 'LIKE'
             )
          );
       }
       $args['meta_query'] = $meta_query;
       query_posts($args);
    Thread Starter duucmeister

    (@duucmeister)

    Hi there,
    Yes I did. I managed to find out what the array is:
    Array ( [0] => Array ( [relation] => OR [0] => Array ( [key] => city [value] => VALUE [compare] => LIKE ) [1] => Array ( [key] => city2 [value] => VALUE [compare] => LIKE ) ) )

    VALUE being the value the frontend users submit when using the form

    But as mentioned it now echoes all the entries in the database, and I don’t know how to fix it :’-(

    Getting all entries indicates that the argument array to the query is not correct. Please print a few lines from your code, starting where you set the arguments array and ending with your query_posts() call.

    Thread Starter duucmeister

    (@duucmeister)

    Please find the code below:

    if ($_POST['achternaam'] != '') { $this->input_name = $_POST['achternaam']; }
    if ($_POST['provincie'] != '' && $_POST['provincie'] != 'Alle provincies') { $meta_query[] = array('key' => 'provincie', 'value' => $_POST['provincie'], 'compare' => 'LIKE'); }
    if ($_POST['modaliteit'] != '') { $meta_query[] = array('key' => 'modality', 'value' => $_POST['modaliteit'], 'compare' => 'LIKE'); }
    if ($_POST['plaats'] != '') {
       $meta_query[] = array('relation' => 'OR',
          array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE'),
          array('key' => 'city2', 'value' => $_POST['plaats'], 'compare' => 'LIKE')
       );
    }
    
    $args = array(
        'role'            => 'Therapeut',
        'posts_per_page'  => 1000,
        'exclude'         => array(1),
        'meta_query'      => $meta_query
    );

    Thread Starter duucmeister

    (@duucmeister)

    BTW: achternaam is Dutch for last name, provincie for province, modaliteit for modality and plaats for city

    You have the $meta_query array nested one level too deep. Try changing this:

    $meta_query[] = array('relation' => 'OR',

    to this:

    $meta_query = array('relation' => 'OR',
    Thread Starter duucmeister

    (@duucmeister)

    I tried it and now nothing gets echoed…

    OK. Please dump out the $args array like this:

    print_r('<pre>ARGS: ');print_r($args);print_r('</pre>');

    and post the output.

    Thread Starter duucmeister

    (@duucmeister)

    Hi there!

    When I keep the []’s in place the output is:

    ARGS: Array
    (
        [role] => Therapeut
        [posts_per_page] => 1000
        [exclude] => Array
            (
                [0] => 1
            )
    
        [meta_query] => Array
            (
                [0] => Array
                    (
                        [relation] => OR
                        [0] => Array
                            (
                                [key] => city
                                [value] => Amsterdam
                                [compare] => LIKE
                            )
    
                        [1] => Array
                            (
                                [key] => city2
                                [value] => Amsterdam
                                [compare] => LIKE
                            )
    
                    )
    
            )
    
    )

    When I remove the []’s no new output shows up

    Is ‘role’ a taxonomy? If so, try using ‘therapeut’ instead of ‘Therapeut’ because the taxonomy slug is what is needed.

    Sorry, I did not notice the ‘modaliteit’ part of the $meta_query. Let me try a test and get back to you.

    This code should produce the proper $meta_query array:

    if ($_POST['achternaam'] != '') { $this->input_name = $_POST['achternaam']; }
    if ($_POST['provincie'] != '' && $_POST['provincie'] != 'Alle provincies') { $meta_query[] = array('key' => 'provincie', 'value' => $_POST['provincie'], 'compare' => 'LIKE'); }
    if ($_POST['modaliteit'] != '') { $meta_query[] = array('key' => 'modality', 'value' => $_POST['modaliteit'], 'compare' => 'LIKE'); }
    if ($_POST['plaats'] != '') {
       $meta_query['relation'] = 'OR';
       $meta_query[] = array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
       $meta_query[] = array('key' => 'city2', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
    }
    
    $args = array(
        'role'            => 'therapeut',
        'posts_per_page'  => 1000,
        'exclude'         => array(1),
        'meta_query'      => $meta_query
    );

    However, I think it will produce unexpected results if both ‘plaats’ and ‘modaliteit’ are present. The ‘OR’ condition will apply to all three keys, not just the two ‘city’ keys. There is currently no way to have the conditional apply to just a subset of the keys.

    Of course, this may not be a problem unless ‘modaliteit’ is required along with one of the two city keys.

    I think you can use filters to do what you want. First, add these two filters to your functions.php:

    function mam_posts_join ($join) {
       global $mam_global_join;
       if ($mam_global_join) $join .= " $mam_global_join";
       return $join;
    }
    function mam_posts_where ($where) {
       global $mam_global_where;
       if ($mam_global_where) $where .= " $mam_global_where";
       return $where;
    }
    add_filter('posts_join','mam_posts_join');
    add_filter('posts_where','mam_posts_where');

    Then, use the code below to create the $args array for your query:

    if ($_POST['achternaam'] != '') { $this->input_name = $_POST['achternaam']; }
    if ($_POST['provincie'] != '' && $_POST['provincie'] != 'Alle provincies') { $meta_query[] = array('key' => 'provincie', 'value' => $_POST['provincie'], 'compare' => 'LIKE'); }
    if ($_POST['plaats'] != '') {
       $meta_query['relation'] = 'OR';
       $meta_query[] = array('key' => 'city', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
       $meta_query[] = array('key' => 'city2', 'value' => $_POST['plaats'], 'compare' => 'LIKE');
    }
    if ($_POST['modaliteit'] != '') {
       $mam_global_join = " JOIN $wpdb->postmeta pm3 ON (pm3.post_id = $wpdb->posts.ID)";
       $mam_global_where = " AND pm3.meta_key = 'modality' AND pm3.meta_value = '{$_POST['modaliteit']}'";
    }
    
    $args = array(
        'role'            => 'Therapeut',
        'posts_per_page'  => 1000,
        'exclude'         => array(1),
        'meta_query'      => $meta_query
    );
Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Can't get a WP query working’ is closed to new replies.