WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Storing multiple meta_query arrays before the query (2 posts)

  1. SpankMarvin
    Member
    Posted 3 years ago #

    I'm having a senior moment with this, and think there must be a simple way to do this.

    I have a gallery that is accepting multiple $_GET values to filter content. This content is used in meta_query arrays to filter custom post data. This is simple enough when I place the arrays inside the meta_query array. Everything works as expected. However, I want to be able to have an author text search override a drop-down name select, meaning that I would love to house all of my separate meta_query arrays in a pre-defined variable, then just pop that into the wp_query object.

    the problem with just adding arrays to something like $filter[] is that the keys stop the query from executing properly. array_values() doesn't help either.

    Can anyone help me think of an idea?

    Thank you in advance.

    John

  2. SpankMarvin
    Member
    Posted 3 years ago #

    Turns out the solution was for me to stop, sleep and look again at my code :)

    Basically, run through the GET paramaters passed from the form, and then add to an array like so:

    if($_GET['art_filtername_text']){
    							$filters[] = array('key' => 'art_filtername', 'value' => $_GET['art_filtername_text'], 'compare' => 'LIKE' );
    
    						} else if($_GET['art_filtername']){
    							$filters[] = array('key' => 'art_filtername', 'value' => $_GET['art_filtername']);
    
    						}
    
    						if($_GET['art_filterprice']){
    							$filters[] = array('key' => 'art_price_regular', 'value' => array($price[0], $price[1]), 'type'=>'numeric', 'compare' => 'BETWEEN' );
    						}

    and then call this array as the value of 'meta_query':

    'meta_query' => $filters

    This now works as expected.

Topic Closed

This topic has been closed to new replies.

About this Topic