Php 如何按自定义键对带有帖子的Wordpress数组进行排序
我想按自定义键“过期日期”对包含posts的数组进行排序Php 如何按自定义键对带有帖子的Wordpress数组进行排序,php,wordpress,Php,Wordpress,我想按自定义键“过期日期”对包含posts的数组进行排序 function kursy_wg_daty() { $a = 0; $todayDate = date("Y-m-d"); echo "<ul>"; while($a < 20) { if($a == 0) { // posts with single expiration_date
function kursy_wg_daty() {
$a = 0;
$todayDate = date("Y-m-d");
echo "<ul>";
while($a < 20) {
if($a == 0) {
// posts with single expiration_date
$args = array("posts_per_page" => 20, "meta_key" => 'expiration_date');
$posts_array = get_posts($args);
foreach($posts_array as $post){
$expiration_date = get_post_meta( $post->ID, 'expiration_date_'.$a, true );
$price = get_post_meta( $post->ID, 'price_'.$a, true );
$only_date = substr($expiration_date, 0, 10);
$time = strtotime($only_date);
$newFormatDate = date('Y-m-d', $time);
if ($newFormatDate > $todayDate)
{
echo "<li> <a class='title' href=" . post_permalink($post) . ">" . $post->post_title . "</a> | " . $post->expiration_date . ", price: " . $post->price . " zł " . "</li>";
}
}
} else {
// posts with more than one expiration_date
$args = array("posts_per_page" => 20, "meta_key" => "expiration_date" . "_" . $a);
$posts_array = get_posts($args);
foreach($posts_array as $post) {
$expiration_date = get_post_meta( $post->ID, 'expiration_date_'.$a, true );
$price = get_post_meta( $post->ID, 'price_'.$a, true );
$only_date = substr($expiration_date, 0, 10);
$time = strtotime($only_date);
$newFormatDate = date('Y-m-d', $time);
if ($newFormatDate > $todayDate)
{
echo "<li> <a class='title' href=" . post_permalink($post) . ">" . $post->post_title . "</a> | " . $expiration_date . ", price: " . $price . " zł " . "</li>";
}
}
}
$a++;
}
echo "</ul>";
}
函数kursy_wg_daty(){
$a=0;
$todayDate=日期(“Y-m-d”);
回声“- ”;
而($a<20){
如果($a==0){
//只有一个到期日的帖子
$args=array(“每页发布”=>20,“元密钥”=>“过期日期”);
$posts\u array=get\u posts($args);
foreach($posts\u数组作为$post){
$expiration\u date=get\u post\u meta($post->ID,'expiration\u date'.$a,true);
$price=get\u post\u meta($post->ID,'price'.$a,true);
$only_date=substr($expiration_date,0,10);
$time=strottime($only_date);
$newFormatDate=日期($Y-m-d',$time);
如果($newFormatDate>$todayDate)
{
echo“
- |”。$post->expiration_date.”,price:“$post->price.zł”。 ”; } } }否则{ //超过一个到期日的帖子 $args=array(“每页发布”=>20,“元密钥”=>“过期日期””。$a); $posts\u array=get\u posts($args); foreach($posts\u数组作为$post){ $expiration\u date=get\u post\u meta($post->ID,'expiration\u date'.$a,true); $price=get\u post\u meta($post->ID,'price'.$a,true); $only_date=substr($expiration_date,0,10); $time=strottime($only_date); $newFormatDate=日期($Y-m-d',$time); 如果($newFormatDate>$todayDate) { echo“
- |”。$expiration_date.”,price:“.price.”zł“ ”; } } } $a++; } 回声“
- 库尔斯A|2021-04-10(08:30-9:45),价格:190 zł
- 库尔斯B|2021-04-09(12:00-13:00),价格:359 zł
- 库尔斯C|2021-04-16(08:30-9:45),价格:150 zł
- 库尔斯2021-04-15(09:00-11:00),价格:399 zł
- 库尔德2021-04-08(11:00-12:30),价格:370欧元
- 库尔德2021-04-12(11:00-12:30),价格:230欧元
- 库尔德2021-04-17(09:00-11:00),价格:180兹罗
- 库尔德2021-04-08(11:00-12:30),价格:370欧元
- 库尔斯B|2021-04-09(12:00-13:00),价格:359 zł
- 库尔斯A|2021-04-10(08:30-9:45),价格:190 zł
- 库尔德2021-04-12(11:00-12:30),价格:230欧元
- 库尔斯2021-04-15(09:00-11:00),价格:399 zł
- 库尔斯C|2021-04-16(08:30-9:45),价格:150 zł
- 库尔德2021-04-17(09:00-11:00),价格:180兹罗
这个问题与我前面的问题相关创建一个
数组
并将所有值推送到该数组,然后按日期时间对数组排序。请尝试下面的代码
function function kursy_wg_daty() {
$a = 0;
$todayDate = date("Y-m-d");
$postIds = array();
while($a < 20) {
if($a == 0) {
// posts with single expiration_date
$args = array("posts_per_page" => 20, "meta_key" => 'expiration_date');
$posts_array = get_posts($args);
if( !empty( $posts_array ) ){
foreach($posts_array as $post){
$expiration_date = get_post_meta( $post->ID, 'expiration_date_'.$a, true );
$price = get_post_meta( $post->ID, 'price_'.$a, true );
$only_date = substr($expiration_date, 0, 10);
$time = strtotime($only_date);
$newFormatDate = date('Y-m-d', $time);
$postIds[$post->ID]['datetime'] = $newFormatDate;
$postIds[$post->ID]['id'] = $post->ID;
$postIds[$post->ID]['post_title'] = $post->post_title;
$postIds[$post->ID]['link'] = get_the_permalink();
$postIds[$post->ID]['expiration_date'] = $expiration_date;
$postIds[$post->ID]['price'] = $price;
}
}
} else {
// posts with more than one expiration_date
$args = array("posts_per_page" => 20, "meta_key" => "expiration_date" . "_" . $a);
$posts_array = get_posts($args);
if( !empty( $posts_array ) ){
foreach($posts_array as $post) {
$expiration_date = get_post_meta( $post->ID, 'expiration_date_'.$a, true );
$price = get_post_meta( $post->ID, 'price_'.$a, true );
$only_date = substr($expiration_date, 0, 10);
$time = strtotime($only_date);
$newFormatDate = date('Y-m-d', $time);
$postIds[$post->ID]['datetime'] = $newFormatDate;
$postIds[$post->ID]['id'] = $post->ID;
$postIds[$post->ID]['post_title'] = $post->post_title;
$postIds[$post->ID]['link'] = get_the_permalink();
$postIds[$post->ID]['expiration_date'] = $expiration_date;
$postIds[$post->ID]['price'] = $price;
}
}
}
$a++;
}
array_multisort( array_map( 'strtotime', array_column( $postIds, 'datetime' ) ), SORT_ASC, $postIds );
echo "<ul>";
foreach ( $postIds as $key => $post ) {
if ( strtotime( $post['datetime'] ) > strtotime( $todayDate ) ){
echo "<li> <a class='title' href=" . $post['link'] . ">" . $post['post_title'] . "</a> | " . $post['expiration_date'] . ", price: " . $post['price'] . " zł " . "</li>";
}
}
echo "</ul>";
}
函数kursy_wg_daty(){
$a=0;
$todayDate=日期(“Y-m-d”);
$postIds=array();
而($a<20){
如果($a==0){
//只有一个到期日的帖子
$args=array(“每页发布”=>20,“元密钥”=>“过期日期”);
$posts\u array=get\u posts($args);
如果(!空($posts\u数组)){
foreach($posts\u数组作为$post){
$expiration\u date=get\u post\u meta($post->ID,'expiration\u date'.$a,true);
$price=get\u post\u meta($post->ID,'price'.$a,true);
$only_date=substr($expiration_date,0,10);
$time=strottime($only_date);
$newFormatDate=日期($Y-m-d',$time);
$postIds[$post->ID]['datetime']=$newFormatDate;
$postIds[$post->ID]['ID']=$post->ID;
$postds[$post->ID]['post\u title']=$post->post\u title;
$postIds[$post->ID]['link']=获取永久链接();
$postIds[$post->ID]['expiration\u date']=$expiration\u date;
$postIds[$post->ID]['price']=$price;
}
}
}否则{
//超过一个到期日的帖子
$args=array(“每页发布”=>20,“元密钥”=>“过期日期””。$a);
$posts\u array=get\u posts($args);
如果(!空($posts\u数组)){
foreach($posts\u数组作为$post){
$expiration\u date=get\u post\u meta($post->ID,'expiration\u date'.$a,true);
$price=get\u post\u meta($post->ID,'price'.$a,true);
$only_date=substr($expiration_date,0,10);
$time=strottime($only_date);
$newFormatDate=日期($Y-m-d',$time);
$postIds[$post->ID]['datetime']=$newFormatDate;
$postIds[$post->ID]['ID']=$post->ID;
$postds[$post->ID]['post\u title']=$post->post\u title;
$postIds[$post->ID]['link']=获取永久链接();
$postIds[$post->ID]['expiration\u date']=$expiration\u date;
$postIds[$post->ID]['price']=$price;
}
}
}
$a++;
}
数组\多排序(数组\映射('strotime',数组\列($postIds,'datetime')),排序\ ASC,$postIds);
回声“- ”;
foreach($key=>$post的posteds){
if(strottime($post['datetime'])>strottime($todayDate)){
echo“
- |”。$post['expiration_date']”,price:“.post['price']”zł”<
global $wpdb; $table_name = $wpdb->prefix . 'postmeta'; $field_name = 'post_id'; $user_id = get_current_user_id(); $get_all_posts = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE `meta_key` LIKE '%expiration_date%' ORDER BY meta_value ASC " ); $post_ids = $wpdb->get_results( $get_all_posts, ARRAY_A ); echo "<ul>"; foreach( $post_ids as $postmeta ){ $expiration_date = get_post_meta( $postmeta['post_id'], $postmeta['meta_key'], true ); $last_value = substr($postmeta['meta_key'],-1); if( is_numeric( $last_value ) ){ $price = get_post_meta( $postmeta['post_id'], 'price_'.$last_value, true ); }else{ $price = get_post_meta( $postmeta['post_id'], 'price', true ); } $only_date = substr($expiration_date, 0, 10); $time = strtotime($only_date); $newFormatDate = date('Y-m-d', $time); echo "<li><a class='title' href=" . get_the_permalink( $postmeta['post_id'] ) . ">" . get_the_title( $postmeta['post_id'] ) . "</a> | " . $newFormatDate . ", price: " . $price . " zł " . "</li>"; } echo "</ul>";