Can't get an relation or to work in meta_query

Support MB Views Can't get an relation or to work in meta_queryResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #43579
    Jai IvarssonJai Ivarsson
    Participant

    I'm trying to get a custom query working in a view for a "tour" CPT. I want either all the tours in the future, or all the past ones and ones that don't have a departure date set. I can successfully get all the past ones, or all the ones with no date set but I can't get them both at the same time. I think I have the syntax right but it isn't working. Anyone able to help?

    {% if date_tense == 'future' %}
    	{% set date_query = [{
    			key: 'departure_date',
    			value: 'now'|date("Y-m-d"),
    			compare: '>=',
    			type: 'DATE'
    		}] %}
    {% else %}
    	{% set date_query = [
    		'relation': 'OR',
    		{
    			key: 'departure_date',
    			value: 'now'|date("Y-m-d"),
    			compare: '<',
    			type: 'DATE'
    		},
    		{
    			key: 'departure_date',
    			compare: 'NOT EXISTS',
    			type: 'DATE'
    		}] %}
    {% endif %}
    {% set query_args = { post_type: 'tour', posts_per_page: -1, meta_query: date_query	} %}
    #43588
    PeterPeter
    Moderator

    Hello,

    The array and object in Twig are different a bit from PHP. You can change the code to this one:

    {% set date_query = {
    	relation: 'OR',
    	0: {
    		key: 'departure_date',
    		value: 'now'|date("Y-m-d"),
    		compare: '<',
    		type: 'DATE'
    	},
    	1: {
    		key: 'departure_date',
    		compare: 'NOT EXISTS',
    		type: 'DATE'
    	}
    } %}
    #43595
    Jai IvarssonJai Ivarsson
    Participant

    Thank you Peter!

    Makes perfect sense once I saw it.

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