• Hi,

    I have this query to get custom posts depending on what value the custom field wpcf-listing-status is set to and the results are sorted using the custom field wpcf-sold-date and the post date.

    Since the upgrade to 4.2 the query is no longer working and the results seem to be random.

    Here is the 4.1 query arguments:

    $args = array(
    				'post_type' => 'condo',
    				'post_status' => 'publish',
    				'posts_per_page' => '12', // you may edit this number
    				'paged'					=> $paged,
    				'meta_query'		=> array ( array ('key' => 'wpcf-listing-status', 'value' => '3', 'compare' => '='),),
    				'orderby' 			=> array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ),
    				'meta_key' 			=> 'wpcf-sold-date'
    			);

    I have read the posts about having to update the meta_query and orderby fields, but I can’t get the same results as before.
    I have updated my query, but it still isn’t working, could you please help get it working.

    Here is the updated query:

    $args = array(
        'post_type' => 'condo',
        'post_status' => 'publish',
        'posts_per_page' => '12', // you may edit this number
        'paged'                 => $paged,
        'meta_query'        => array (
                                                'listing_clause' =>  array (
                                                        'key' => 'wpcf-listing-status',
                                                        'value' => '3',
                                                        'compare' => '='
                                                    ),
                                            ),
        'orderby'           => array(
                                                'meta_value_num' => 'DESC',
                                                'date' => 'DESC'
                                            ),
        'meta_key'          => 'wpcf-sold-date',
        'meta_type'         => 'DATE'
    );

    Here is what it shows as the Select that this produces:

    SELECT SQL_CALC_FOUND_ROWS ms15_posts.ID
    FROM ms15_posts
    INNER JOIN ms15_postmeta ON ( ms15_posts.ID = ms15_postmeta.post_id )
    INNER JOIN ms15_postmeta AS mt1 ON ( ms15_posts.ID = mt1.post_id )
    WHERE 1=1 AND ( ms15_postmeta.meta_key = 'wpcf-sold-date' AND ( ( mt1.meta_key = 'wpcf-listing-status' AND CAST(mt1.meta_value AS CHAR) = '3' ) ) ) AND ms15_posts.post_type = 'condo' AND ((ms15_posts.post_status = 'publish'))
    GROUP BY ms15_posts.ID
    ORDER BY ms15_posts.menu_order, ms15_postmeta.meta_value+0 DESC, ms15_posts.post_date DESC LIMIT 0, 12

    which has the wpcf-sold-date in the WHERE clause instead of in the ORDER By clause.

    So, I then tried:

    $args = array(
        'post_type' => 'condo',
        'post_status' => 'publish',
        'posts_per_page' => '12', // you may edit this number
        'paged'                 => $paged,
        'meta_query'        => array (
                                                'listing_clause' =>  array (
                                                        'key' => 'wpcf-listing-status',
                                                        'value' => '3',
                                                        'compare' => '='
                                                    ),
                                            ),
        'orderby'           => array(
                                                array (
                                                    'meta_value_num' => 'DESC',
                                                    'meta_key'          => 'wpcf-sold-date',
                                                    'meta_type'         => 'DATE'
                                                ),
                                                'date' => 'DESC'
                                            ),
    );

    and got this SELECT:

    SELECT SQL_CALC_FOUND_ROWS ms15_posts.ID
    FROM ms15_posts
    INNER JOIN ms15_postmeta ON ( ms15_posts.ID = ms15_postmeta.post_id )
    WHERE 1=1 AND ( ( ms15_postmeta.meta_key = 'wpcf-listing-status' AND CAST(ms15_postmeta.meta_value AS CHAR) = '3' ) ) AND ms15_posts.post_type = 'condo' AND ((ms15_posts.post_status = 'publish'))
    GROUP BY ms15_posts.ID
    ORDER BY ms15_posts.menu_order, ms15_posts.post_date DESC LIMIT 0, 12

    which totally lost the ordering by wpcf-sold-date.

    Any ideas how to get the $arg so it works like it used to in 4.1?

    Thank you.

  • The topic ‘Query not working after 4.2 upgrade’ is closed to new replies.