WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query posts by meta value date (3 posts)

  1. SteveStall
    Member
    Posted 1 year ago #

    [Post title de-capped. Please do not shout at us.]

    Hi there,

    I'm trying to get my posts filtered by post type, category and date (custom field - ACF).
    I think my query is correct, yet no results. By looking into WP post list in Admin area I can confirm the post I'm searching for exists.

    Here is my code and print

    $press = new WP_Query
    			(
    				array
    				(
    					'post_type'    => 'press',
    					'post_status'  => 'publish',
    					'cat'          => 8,
    					'post__not_in' => 413,
    					'meta_query'   => array
    					(
    						array
    						(
    							'key'     => 'sx_press_review_date',
    							'value'   => array( 20110101, 20120101 ),
    							'type'    => 'NUMERIC', // TRIED: DATE, SIGNED, NUMBER
    							'compare' => 'BETWEEN'
    						)
    					),
    					'orderby' => 'meta_value_num',
    					'order' => 'DESC'
    				)
    			);

    part of the print comes like:

    [meta_query] => WP_Meta_Query Object
            (
                [queries] => Array
                    (
                        [0] => Array
                            (
                                [key] => sx_press_review_date
                                [value] => Array
                                    (
                                        [0] => 20110101
                                        [1] => 20120101
                                    )
    
                                [type] => NUMERIC
                                [compare] => BETWEEN
                            )
    
                    )
    
                [relation] => AND
            )
    
        [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND wp_posts.ID NOT IN () AND wp_posts.post_type = 'press' AND (wp_posts.post_status = 'publish') AND ( (wp_postmeta.meta_key = 'sx_press_review_date' AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN '20110101' AND '20120101') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5
        [posts] => Array
            (
            )
    
        [post_count] => 0

    Can anybody tell me if there is something missing?

    Thanks
    Peter

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Strange, 'NUMBER' worked for me. I did have to add 'meta_key' => 'sx_press_review_date' to get the sort order to work.

    My meta_values are formatted 'yyyymmdd'. Could a different format be the problem for you?

  3. SteveStall
    Member
    Posted 1 year ago #

    Hi vtxyzzy,

    Thanks for your help.

    I've managed to get things working by settings the 'post__not_in' value has an array, just as specified in WordPress codex page.

    Thanks
    Peter

Topic Closed

This topic has been closed to new replies.

About this Topic