Support

Account

Home Forums General Issues Order by custom field not working

Solved

Order by custom field not working

  • I have a list of custom post types that I’m displaying, with the intention of displaying them in order of campaign_end_date which is a custom field.

    I’m running a WP_Query with the following attributes:

    
    $crowdfunding_posts = new WP_Query(get_posts(array(
        'post_type'   => 'crowdfunding',
        'numberposts' => 30,
        'meta_key' => 'campaign_end_date',
        'order_by' => 'meta_value',
        'order' => 'ASC',
    )));
    

    I retrieve the raw data with this query

    
    select
    	p.post_title, 
    	pm.meta_value
    from wp_posts p
    inner join wp_postmeta pm on pm.post_id = p.ID
    where pm.meta_key = 'campaign_end_date'
    and p.post_status = 'publish'
    order by pm.meta_value asc
    

    and based on that, the expected order of these posts should be:

    
    POST_ID	POST_TITLE	META_VALUE
    257396	Comic Book Bubble	20220913
    257398	Trailblazers	20220916
    257403	CobbleCritters	20220922
    257404	Kinfire Chronicles: Nights Fall	20220921
    257405	Sea of Legends: Vengeance of the Empires	20220915
    257406	Almost Innocent	20220920
    257862	Weavlings in the Wilds	20220929
    257863	River Trek	20220909
    257864	Bakufu: A Japanese Themed Strategic Card Game	20220920
    257865	Full Sun	20220917
    257866	Diamond Dig	20220920
    257867	Barbaric	20220906
    257868	Dwar7s Legendary Forest	20220921
    

    But on page they’re displaying in the following order.

    
    Comic Book Bubble	20220913
    CobbleCritters	20220922
    Trailblazers	20220916
    Kinfire Chronicles: Nights Fall	20220921
    Sea of Legends: Vengeance of the Empires	20220915
    Almost Innocent	20220920
    Weavlings in the Wilds	20220929
    River Trek	20220909
    Bakufu: A Japanese Themed Strategic Card Game	20220920
    Full Sun	20220917
    Diamond Dig	20220920
    Barbaric	20220906
    Dwar7s Legendary Forest	20220921
    

    I’m sure I’m just doing something wrong in my query, but I’m following the steps listed in the docs (https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/), and I’m seeing what I’m doing wrong. Does anyone have input?

    EDIT: I came back and added the post id for reference and it almost seems like it’s being ordered by that instead of the custom field. That reinforces my belief that I’m missing something, but I’m just not seeing it.

  • Correction, I pasted in the wrong query above.

    The query

    
    select
    	p.ID,
    	p.post_title, 
    	pm.meta_value
    from wp_posts p
    inner join wp_postmeta pm on pm.post_id = p.ID
    where pm.meta_key = 'campaign_end_date'
    and p.post_status = 'publish'
    order by pm.meta_value asc
    

    The data

    
    257867	Barbaric	20220906
    257863	River Trek	20220909
    257396	Comic Book Bubble	20220913
    257405	Sea of Legends: Vengeance of the Empires	20220915
    257873	RoboMon	20220916
    257398	Trailblazers	20220916
    257865	Full Sun	20220917
    257864	Bakufu: A Japanese Themed Strategic Card Game	20220920
    257866	Diamond Dig	20220920
    257874	Honey	20220920
    257406	Almost Innocent	20220920
    257868	Dwar7s Legendary Forest	20220921
    257404	Kinfire Chronicles: Nights Fall	20220921
    257403	CobbleCritters	20220922
    257869	The FOG - Escape from Paradise	20220922
    257862	Weavlings in the Wilds	20220929
    
  • I knew it was something simple. I was using order_by instead of orderby.

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

You must be logged in to reply to this topic.