Support

Account

Home Forums General Issues meta query with two custom types

Solving

meta query with two custom types

  • I’ve created two custom types:
    – art
    – media
    For the art type I’ve created a custom field ‘post object’ which is linked to the media post type media. Multiple selections are allowed.

    So for instance an artwork is linked to the following media items:
    – mixed media
    – painting.

    One the front of the website people can filter art by selecting one or more media-types. So when mixed media is selected, all mixed-media art is shown. When mxed media and painting is selected all art with these two media-types are shown.

    But how do I do this on the front side? So far I have
    ($aCheckedMedia is an array with ID’s of the selected media-types)

    $arrays = array('relation' => 'AND');
    for ($i = 0; $i < count($aCheckedMedia); $i++) {
    	$count = count($arrays);
    	$arrays[$i] = array(
    		'key' => 'categorie',
    		'value' => $aCheckedMedia[$i],
    		'compare' => 'LIKE'
    	);
    }
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $kunst = new WP_Query( array(
    	'post_type' => 'kunst', 
    	'orderby' => 'title', 
    	'order' => 'asc',
    	'posts_per_page' => 4,
    	'meta_key' => 'status',
    	'meta_value' => 'te koop',
    	'meta_query' => $arrays
    ));

    But than only art is shown where both media-types are selected and art with only one of the media-types area not shown. If I use
    $arrays = array('relation' => 'OR');
    all art shows up.

  • Hi @advancedcustomfields@swell.nl

    The relation should be OR to allow for any of the meta_query’s to match. I can’t explain why ALL the art appears.

    Firstly, can you change:
    $arrays[$i] = array(

    to:
    $arrays[] = array(

    I’m not sure if that is an important change, but I don’t think specific indexes are needed here.

    The main issue I can see is that your WP_Query has:

    'meta_key' => 'status',
    	'meta_value' => 'te koop',

    This will override the 'meta_query' => $arrays

    Please only use the 'meta_query' => $arrays

    Thanks
    E

  • Hi Elliot,

    Thank you for your reply. Your solution worked, thanks!

    But I really need the extra ‘status’ parameter. It’s an another custom field where I can mark the item as sold/reserved/for sale.
    How can I combine this with the meta_query?

    Thanks,
    Edwin

  • Hi @advancedcustomfields@swell.nl

    This is a limitation of the WP_Query args.
    If you were to add the status to the meta_query array, then it too would be treated as an ‘OR’.

    It seems that you can’t mix ‘AND’ with ‘OR’ in a meta_query.

    You may need to first find all the posts using the meta_query you have, then loop over the results and remove (unset) the ones which don’t have the correct ‘status’ meta_value.

    Thanks
    E

  • Hi ELliot,

    Thank you again for your reply.
    Unfortunataly I have a paginate_links() on the bottom of the page. I think filtering will break it (count wrong)…

    Do you know a solution for that?

    Thanks
    Edwin

  • Hi @edwin

    I think you can edit the global $wp_query object to modify the number of results and the actual results found.

    But perhaps the easiest solution of all is to use a taxonomy for the ‘status’ this way you can separate it form the meta_query.

    Thanks
    E

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘meta query with two custom types’ is closed to new replies.