Mashup query with two custom fields

Home Forums MapPress Support Mashup query with two custom fields

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13779
    dkingmahoney
    Participant

    Chris: I’m trying to do something simple — a mashup query where two different custom fields must have specified values; let’s say “color=blue” and “size=large”. Both conditions must be satisfied.

    Writing the query the following way seems to produce an “or” result rather than an “and” result:
    [mashup query="meta_value__and=blue,large"]

    And if the query is instead written this way, the first meta_value is ignored:
    [mashup query="meta_value=blue&meta_value=large"]

    Any help?

    Thanks for the great plugin.

    #13781
    Chris
    Keymaster

    Unfortunately, the WP query engine doesn’t provide support for more than one meta value in a query string. Multiple ‘meta_key’ or ‘meta_values’ are ignored (‘meta_value__and’ is also ignored, it’s not a valid keyword).

    One workaround is to use a single value or a taxonomy instead. Or, a ‘save_post’ action can be used to set a single custom field value or category when the post is saved.

    Another option is to create the map using PHP. If the query is passed as an array, WP allows complex mtea queries. You’ll need to add some code to your template:

    <?php
    	$query = array(
    	'post_type' => 'any',
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		array('key' => 'color', 'value' => 'blue'),
    		array('key' => 'size', 'value' => 'large'),
    	)
    );
    $map = new Mappress_Map(array('query' => $query, 'width' => 400, 'height' => 400));
    echo $map->display();
    ?>

    There are some meta query examples here:
    http://codex.wordpress.org/Class_Reference/WP_Query

    #13783
    dkingmahoney
    Participant

    Thanks very much for the quick response, Chris. I’m going to change one of the custom fields to a taxonomy. That’s probably a better approach for my site anyway, as the values for one of the two custom fields could apply beyond the particular custom post type I’m building the query for.

    Thanks again! Cheers.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.