开发者

Sort WordPress posts by a date in a custom field

开发者 https://www.devze.com 2023-03-14 06:03 出处:网络
I am grabbing posts from the WordPress database using this piece of code:开发者_如何转开发 $query = array(

I am grabbing posts from the WordPress database using this piece of code:开发者_如何转开发

$query = array(
    'numberposts' => -1,
    'post_type' => 'post',
    'post_status' => 'publish'
);

$posts = get_posts($query);

I want to order the posts by a date in a custom field.

The custom field is called netr_event_date_start and contains strings formatted like YYYY-MM-DD (for example 2011-10-24).

How can this be achieved? Thanks for your help.


One idea is:

    $args=array(
      'meta_key' => 'netr_event_date_start',
      'orderby'=>'netr_event_date_start',
      'numberposts' => -1,
      'post_type' => 'post',
     'post_status' => 'publish'
  );

  $posts = get_posts($args);

and another

   $args=array(
      'meta_key' => 'netr_event_date_start'
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1,
      'orderby'=>'netr_event_date_start'
   );

   $my_query = null;
   $my_query = new WP_Query($args);

   if( $my_query->have_posts() ) {
     while ($my_query->have_posts()) : $my_query->the_post(); ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号