Skip to:

Meta query in Views

  • robsward


    I’m trying to create a View that uses the following arguments, using meta_query and date_query, but it doesn’t seem to filter at all and displays all posts:

    $args2  = array( 
            'orderby' => 'post_date',
    	'order', 'DESC',
            'meta_query' => array(
                 'relation'  => 'OR',
    	             'key'      => '_bbps_topic_status',
    	             'value'    => '1',
                     'compare'   => 'LIKE'
    	             'key'      => '_bbps_topic_status',
    	             'value'    => '2',
                     'compare'   => 'LIKE'
            'date_query' => array(
    		        'year'  => date( 'Y' ),
    		        'month' => date( 'M' ),
    	bbp_register_view( 'unsolved-tix', __( 'Unsolved Tickets', 'textdomain' ), $args2, true );

    Using just a single meta_key works fine, but in these args, neither the date_query nor meta_query work. Are these unsupported?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Here is the resultant SQL query for your code above:

    FROM wp_posts 
    INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
    INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
    WHERE 1=1 
    AND ( ( YEAR( post_date ) = 2014 ) )
    AND wp_posts.post_type = 'topic'
    AND (wp_posts.post_status = 'publish'
    	OR wp_posts.post_status = 'closed'
    	OR wp_posts.post_status = 'private'
    	OR wp_posts.post_status = 'hidden')
    AND (wp_postmeta.meta_key = '_bbp_last_active_time'
    	OR (mt1.meta_key = '_bbps_topic_status'
    		AND CAST(mt1.meta_value AS CHAR) LIKE '%1%')
    	OR (mt2.meta_key = '_bbps_topic_status'
    		AND CAST(mt2.meta_value AS CHAR) LIKE '%2%') 
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_date DESC
    LIMIT 0, 15

    I think you are hitting a known limitation of WordPress at the moment nesting queries, take a look at Boone’s posts on the improvements coming to WordPress 4.1:

    Meta, Date, and Tax Query improvements in WP 4.1

    …classes currently accept a ‘relation’ parameter, which allows multiple query clauses to be joined with AND or OR. So, for instance, you can query for: posts that are in the ‘Boone’ category AND have the tag ‘Awesome’ AND do not have the term ‘Raphael’ in the favorite_turtle taxonomy. But this syntax only allows for so much expressiveness: you can’t mix AND and OR, and you can’t group clauses. These limitations make it difficult to use these queries in support of complex content filtering…

    Then take a read of the follow up post explaining what has happened since that first post, I haven’t had the time to look at how this changes affect bbPress, they should not break bbPress, but these will be something we will take a look at to a) Ensure bbPress is not affected by these WordPress 4.1 updates and b) How we can leverage these improvements in future versions of bbPress.

    Update on Query improvements in 4.1



    Hi Stephen,

    Thanks so much for the thought-out response. I’ve done a bit more poking around and found that it isn’t necessarily the complexity of the query, but rather the ‘OR’ relation that returns everything. If I replace that OR with AND and use a different meta key for the second array (status is either 0,1 or 2), I get a good list of results.

    Is there another way to ask for an OR relationship between values of a single meta key?






    Since I only had three values, I used the following to get around the ‘OR’ issue:

    'meta_key' => '_bbps_topic_status',
    'meta_value' => '3',
    'meta_compare' => '!='

    However, I now have another question, which is that I want to exclude from this view posts whose last reply author’s role is not keymaster or moderator. I have this logic in a template file to help me style the author link:

    		$reply_post_id		= bbp_get_topic_last_active_id();
    		$reply_author_id 	= bbp_get_reply_author_id( $reply_post_id );
    		$reply_author_role 	= bbp_get_user_display_role( $reply_author_id );
    		if( $reply_author_role == "Moderator" || $reply_author_role == "Keymaster" ) {
    			$role = "moderator";
    		} else {
    			$role = "";

    What I’d like to do is roll this into the query to return a list of topics/threads whose status is not ‘3’, and whose last active user does not belong to either the Moderator or Keymaster groups. Not sure if this is even possible, since one seems like it requires the WP_Users_Query– perhaps I should create a separate meta_key/value pair for the last active user role?

    Thanks again

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