Support

Account

Home Forums General Issues Advance wp_query with conditions…

Solved

Advance wp_query with conditions…

  • I am doing an advanced wp_query that is looking for various custom fields, based on alot of conditions. It isn’t pulling anything up… so I wanted to check that I am doing the query correctly. Below is what I am trying to check, and then the code for what I have tried …

    Please let me know what I am doing wrong in my query….

    1. The first checks if the banner_image2 field exists…
    2. AND we check if the banner_link field exists…
    3. AND we check if the banner_loc = $bannerloc (in this case, = 1)…
    4. AND Then we check if the banner_cats multi select contains any items $pagecategories…
    OR if banner_cats contains value of 1
    5, AND then check if banner_towns = $towncatid
    OR if banner_towns=1;

    // These values outside of the include...
    $bannerloc = 1;
    $sizekey = 'banner_image2';
    $pagecategories =  wp_get_post_categories(); // get_the_category();
    
    // Query Args inside the include...
    $args = array (
    	'post_type' => 'post', 
    	'orderby' => 'rand', 
    	'cat' => 2231, 
    	'posts_per_page' => 1,
    	'meta_query'	=> array(
    		'relation'		=> 'AND',
    			array(
    				'key'	 	=> $sizekey,
    				'value'	  	=> '',
    				'compare' 	=> '!=',
    			),
    			array(
    				'key'	 	=> 'banner_link',
    				'value'	  	=> '',
    				'compare' 	=> '!=',
    			),
    			array(
    				'key'	  	=> 'banner_loc',
    				'value'	  	=> $bannerloc,
    				'compare' 	=> '=',
    			),
    			array(
    				'relation'		=> 'OR',
    					array(
    						'key'	  	=> 'banner_cats',
    						'value'	  	=> $pagecategories,
    						'compare' 	=> 'IN',
    					),
    					array(
    						'key'	  	=> 'banner_cats',
    						'value'	  	=> 1,
    						'compare' 	=> '=',
    					),	
    			),		
    			array(
    				'relation'		=> 'OR',
    					array(
    						'key'	  	=> 'banner_towns',
    						'value'	  	=> $towncatid,
    						'compare' 	=> '=',
    					),
    					array(
    						'key'	  	=> 'banner_towns',
    						'value'	  	=> 1,
    						'compare' 	=> '=',
    					),
    			),
    		),
    );
    
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) : 
    	while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
    	// Code for post here...
    		
    	endwhile; 
    	wp_reset_postdata();
    endif;
    
  • banner_cats is a multiselect field. This field will contain an array of category IDs and will be stored as a serialized array. You can’t do "IN" or "=" here.

    To match this you need to do something like this:

    
    array(
        'key' => 'banner_cats',
        'value' => '"'.$cat_id.'"',
        'compare' => 'LIKE',
    )
    

    Notice the double quotes include in the value. This is so that 1 will only match 1 and not 10, 20.

    To do something resembling an “IN” you would need to loop through the list of categories and create a “LIKE” query for each of them.

    The reason that you get nothing is that the meta queries on banner_cats and banner_towns are retuning nothing.

    Hope that helps.

    ~JH

  • Awesome! That was definitely an issue…

  • The multiple nested meta queries all look good, quite well done, except for what I pointed out which is mostly just understanding out ACF actually stores some fields.

  • I’ve cleaned things up a bit… but now I’m getting an trim error from the meta.php file…

    Here is the error..

    Warning: trim() expects parameter 1 to be string, array given in /home/tipsfromtown/public_html/wp-includes/meta.php on line 1432

    Since I’ve already closed this topic (cause the initial problem was fixed), and noone is responding to this thread anymore, I’ll open a new one with my code…

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

You must be logged in to reply to this topic.