WordPress.org

Ready to get started?Download WordPress

Forums

Advanced WP_Query (2 posts)

  1. colin1388
    Member
    Posted 8 months ago #

    I've gone through the Codex and Google trying to make this query work, but it doesn't want to budge no matter how many examples I follow.

    I'm trying to do a query on a custom field that allows me to select all data with post_type of "homes" that has a "square_footage" >= [number] AND <= [number]

    Here is what my current query looks like

    $args = array(
    	  'post_type' => "homes",
    	  'post_status' => 'publish',
    	  'posts_per_page' => -1,
    	  'meta_query' => array(
    			'key' => 'square_feet',
    			'value' => array( $nums[0], $nums[1] ),
    			'compare' => 'BETWEEN'
    		);
    	);

    When I do a print_r on the $args varialbe, I get the following:

    Array
    (
        [post_type] => homes
        [post_status] => publish
        [posts_per_page] => -1
        [meta_query] => Array
            (
                [relation] => AND
                [0] => Array
                    (
                        [key] => square_feet
                        [value] => Array
                            (
                                [0] => 1000
                                [1] => 1250
                            )
    
                        [compare] => BETWEEN
                    )
    
            )
    
    )

    I would greatly appreciate any help :) Thanks!

  2. vtxyzzy
    Member
    Posted 8 months ago #

    Try adding the 'type' => 'numeric' parameter to your meta_query:

    'meta_query' => array(
    			'key' => 'square_feet',
    			'value' => array( $nums[0], $nums[1] ),
    			'type' => 'numeric',
    			'compare' => 'BETWEEN'
    		);

Reply

You must log in to post.

About this Topic