Forums

[resolved] Trouble using orderby and meta_query for a Custom Post Type (4 posts)

  1. Jeff Kamo
    Member
    Posted 1 year ago #

    Hi, I having a problem where I have a custom post type called webinars. I am trying to list all upcoming webinars, and have them ordered by the custom meta field called webinar_startDate.

    Using the following query, I was able to return the webinars succesfully excluding the old webinars. However, they still come out in the order they were published, and not by webinar_startDate.

    <?php $my_array =  array(
    	'meta_query' => array(
    			array(
    				'key' => 'webinar_startDate',
    				'value' => date("Y-m-d H:i:s"),
    				'compare' => '>=',
    				'type' => 'DATETIME'
    			)
    	),
    	'orderby' => 'meta_value',
    	'post_type' => 'webinars',
    	'posts_per_page' => 20,
    	'order' => 'ASC'
    ); ?>

    I suspect that due to the change from 3.0 to 3.1, the use of orderby => meta_value is probably different, but I can't find within the WordPress documentation what that change is.

    Can anyone help? Thanks in advance.

  2. Jeff Kamo
    Member
    Posted 1 year ago #

    I received my answer.

    I was simply confused by the WordPress documentation which described 'meta_value' and 'meta_key' as being depreciated. But the fact is, I can (and must) use both in conjunction with meta_query to achieve my desired effect.

    Use meta_query to query and capture the desired posts. Then, use meta_value and meta_key to manipulate those posts as I please. See below...

    [Code moderated as per the Forum Rules. Please use the pastebin]

  3. JsonB123
    Member
    Posted 1 year ago #

    Hi Jeff, would you mind posting what you did via a link to PasteBin? The forum has removed it permanently and I'm having a similar issue. I'd like to see how you achieved this functionality.

    Thanks!

  4. Jeff Kamo
    Member
    Posted 1 year ago #

    Oh, I was not aware of that. Okay, this is my final code: http://pastebin.com/KE9P5efL

Topic Closed

This topic has been closed to new replies.

About this Topic