dongzang7182 2015-09-15 16:25
浏览 41

在Wordpress中的meta_key数组中按事件日期订购自定义帖子类型

I've looked into a lot of answers and solutions, and I'm having difficulty implementing this.

My goal: In Wordpress, I want to order the posts from a custom post type (Event) to display according to a custom date field. I've seen other solutions for this, but they're not working for me, as my custom key is an array in the meta value, and the solutions were not.

I can't change the core of the code because the entire site runs on it and wasn't created by me.

The meta key I'm using is _my_meta, and to get post meta I use get_post_meta($post->ID,'_my_meta',TRUE); which I will put into a variable ($meta). All the fields I call are in an array, so $meta['img'] might be a custom image, and $meta['date'] might be a custom date.

Currently, I have everything working and pulling properly from a custom WP_Query loop. I just want the posts to pull and order themselves by a custom text field of date.

Here is the loop I am using:

<?php
$args = array(
  'post_type' => 'event',
  'order' => 'ASC',
  'orderby' => 'meta_value',
  'meta_key' => '_my_meta',
  'meta_query' => array(
    array(
      'key' => '_my_meta',
      'value' => date('Ymd'),
      'compare' => '>='
    )
  )
);
$new = new WP_Query( $args );
while ($new->have_posts()) : $new->the_post();
?>

So, I'm pulling from the 'event' post type, in ascending order, ordering by my meta value. The custom field is just a text box, in which I have put a date in the format of "20150809" (which equals date('Ymd')).

The meta_key is _my_meta, however I really want the information from get_post_meta($post->ID,'_my_meta',TRUE)['date']. Is this even possible? I'm positive the method being used in functions.php isn't ideal, but it's what I have to work with.

I've tried changing meta_value to meta_value_num, and it moves things around but not in an ideal way. Within a month, the date seems to be in order, but the months are all out of order.

I put in some arbitrary dates, and with this current setup, it's showing as:

November 4th
November 6th
December 5th
August 9th

Any and all help appreciated. Thank you!!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示