Ordering Post by Meta Key (Not Working)

Had same issue. Have spent like few hours searching for the problem and it turned out to be just a typo: instead of incorrect order_by you should use orderby – without the underscore. So, this should work: $args = array( ‘post_type’ => ‘event’, ‘meta_key’ => ‘events_startdate’, ‘orderby’ => ‘meta_value’, ); $post_query = new WP_Query( $args … Read more

Order by Date Custom Field

I have the code below (started from yours) setup as a shortcode and it works fine. When I change the direction ‘ASC’ to ‘DESC’, the sort order changes as well, so it is not ‘luck’ that posts are in the correct order. It assumes that the dates are stored yyyymmdd. NOTE that one must remove … Read more

Trying to get custom field data in a simple plugin

You don’t have the $post object, but trying to use it in the code ($post->ID). Use get_the_ID() function instead: <?php while ( $cat_query->have_posts() ) : $cat_query->the_post(); // $gread = get_post_meta( $post->ID, ‘read_time’, true ); // Wrong $gread = get_post_meta( get_the_ID(), ‘read_time’, true ); // Right endwhile; } From now forth always enable debugging when you … Read more