wordpress高级自定义字段按日期选择器排序帖子

问题描述 投票:1回答:2

对于那些熟悉ACF插件的人......

我有一些活动帖子,目前正按照博士后的顺序显示(见下面的代码)。我希望它们按日期选择器指定的顺序显示。

任何人都可以告诉我在下面修改什么 - 我已经尝试了网站上的文档,但我的PHP是基本的。

它说我需要添加

'orderby' => 'meta_value_num',

但没有快乐。

<?php function le_whatson_aside() {

//THis loop is for the CPT
$args = array(
    'post_type' => 'events', // enter your custom post type
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'posts_per_page'=> '10',  // overrides posts per page in theme settings
        'tax_query' => array(
        array(
            'taxonomy' => 'audience', //name of custom taxonomy
            'field' => 'slug',
            'terms' => 'everyone' //name of category
        )
    )

    );

$loop = new WP_Query( $args );
if( $loop->have_posts() ):
    ?>

    <div>
        <h2>What's On</h2>
    </div>
    <div class="whatson entry-content"> 
        <?php   
        while( $loop->have_posts() ): $loop->the_post(); global $post;
            ?>
            <p class="whatson-date"><?php echo date("dS F Y",strtotime(get_field('date')));?></p>
            <a  href="<?php echo get_permalink() ?>"><h4 class="whatson-title"><?php echo get_the_title(); ?></h4></a>

            <?php
        endwhile;
        ?>
    </div>

<?php
endif; }

谢谢大家。

wordpress
2个回答
0
投票

尝试

orderby=date or `post_date`

如果不是最简单的方法是将自定义字段'startdate'保存为unix时间戳。为此,请将以下内容添加到主题的functions.php中

// CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'startdate', true);

    if($startdate) {
        $dateparts = explode('/', $startdate);
        $newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
        update_post_meta($post_id, 'unixstartdate', $newdate1  );
    }
}
 }
     add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

该做的:

$today = time();

$args = array(
'post_type' => 'events',
'posts_per_page' => 5,

'meta_query' => array(
    array(
        'key' => 'unixstartdate',
        'compare' => '>=',
        'value' => $today,
    )
),

'meta_key' => 'startdate',
'orderby' => 'meta_value',
'order' => 'ASC',
);

$query = new WP_Query( $args );
$events = $query->posts;

Got it from here


0
投票

我花了几个小时寻找这个,我可以确认它有效。请参阅下面的代码。

如果您正在尝试在包含其他循环的页面上进行循环,其中包含一堆模板部件,并且您还希望按类别排序,则会:

    $today = time();

    $the_query = new WP_Query( array(
    'post_type' => 'events',
    'posts_per_page' => 3,
    'meta_query' => array(
        array(
            'key' => 'start',
            'value' => $today,
            'compare' => '>=',
        )
    ),

    'tax_query' => array(
        array (
        'taxonomy' => 'the_taxonomy',
        'field' => 'slug',
        'terms' => 'the-name'
        )
    ),
    'meta_key' => 'start',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    ) );
while ( $the_query->have_posts() ) :
    $the_query->the_post();
        get_template_part( 'content-events' );
endwhile;
wp_reset_postdata();

当然,你必须预先包含Unix这个功能。

function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'start', true);
    if($startdate) {
        $dateparts = explode('_', $startdate);
        $newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
        update_post_meta($post_id, 'unixstartdate', $newdate1  );
    }
}
 }
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
© www.soinside.com 2019 - 2024. All rights reserved.