数组 - Orderby - 类型

时间:2016-03-12 18:40:43

标签: php html arrays wordpress types

设置程序概述,运行时间超过四(4)天:周三,周四,周五,周六。所有活动(电影,活动,研讨会,研讨会++)都列在post type event内。

我打算将查询分成每一天,这意味着args应设置date

最终结果应与此类似:

Wednesday

11:00

<---- Events which starts at 11:00 ---->

12:00

<---- Events which starts at 12:00 ---->

下一页:

Thursday

11:00

<---- Events which starts at 11:00 ---->

12:00

<---- Events which starts at 12:00 ---->

等等。

我已经开始使用PHP了,但在我意识到我需要将args与使用Types的信息结合起来的地方停了下来。

每个活动都有post type event并定义了date。制作文章时,这不是常规WordPress post date。意义orderby date将毫无用处。

摘录:

$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
);

$the_query = new WP_Query($args); 

注意:现在,如上所述,它会在post date之后进行排序,但这不正确,并且不起作用。

<?php if ( $the_query->have_posts() ) : ?> <!-- Listing all the events -->

<div id="isotope-list">

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

<div class="col-md-4"> <!-- Each event should be in this div -->

<?php
$value = types_render_field('dato');
if ( !empty($value) ) { 
echo do_shortcode( '[types field="dato" format="g:i"][/types]' ); 
echo " |";
}
else { echo ""; }
?>

</div> <!-- End col-md-4  -->
<?php endwhile;  ?>
</div> <!-- End isotope-list -->
<?php endif; ?>

我认为应该解释问题,我的思考方式,我需要帮助的地方和我尝试过的地方。如果您有任何疑问,请询问。

可在此处找到该页面:http://vekvemedia.no/oppdrag/dokfilm/ (滚动至计划)

抱歉该网站是挪威语。翻译:

Onsdag - 周三,Torsdag - 周四,Fredag​​ - 周五,Lørdag - 周六。

更新

$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_query'        => array(
           'key' => 'dato',
           'value' => date("Y-m-d H:i:s"),
           'compare' => '>=',
           'type' => 'date'
    ),
    'orderby' => 'meta_value', 
    'meta_key' => 'dato',
    'order' => 'DESC',
    );

更新2

在大卫的帮助下

<?php
    $args = array(
        'post_type' => 'event',
        'posts_per_page' => -1,
    );

    $the_query = new WP_Query($args); 
?>

<?php $x= 0;

if ( $the_query->have_posts() ) : ?> <!-- Listing all the events -->

<div id="isotope-list">

    <!-- open the first day -->
    <div id="isotope-day">
        <div class="col-md-12">

        <?php while ( $the_query->have_posts() ) : $the_query->the_post();

            $newtime = false;
            $date = types_render_field('dato', array("format"=>"U"));//get a unix date, i'm not sure of the dateformat you are using but it should be ok..if not ref strtotime for your date format
            $now = strtotime('now');

            if($date < $now){
                continue;
            }

            //do we need a new day?? yes if not the same date!
            if( date('d/m/Y', $date) !== $curdate  || !isset($curdate) ){
                $curday = date('l', $date );
                $curdate= date('d/m/Y', $date);
                $newdate= true; 
                $x++;
            }

            //do we need a new time??
            if( date('H', $date ) != $curtime || !isset($curtime) ){
                $curtime = date('H', $date );
                $newtime= true; 
            }

        ?>

        <?php if($newday): ?>

        <?php if ($x>0):?>
        </div> <!-- End previous day when adding a new one except in first instance -->
    </div> <?php endif; ?>

    <div id="isotope-day">
        <div class="col-md-12">
            <h1><?php echo $curday.' '.$curdate; ?></h1>

            <?php endif; ?>
            <?php if($newtime) : ?>
            <h2><?php echo $curtime; ?>:00</h2>
            <?php endif; ?>

            <div class="col-md-4">
                <a href="#">
                    <div class="content grid lefttext maximg littluft">
                        <div class="red-overlay">
                            <figure>
                                <?php if ( has_post_thumbnail() ) { the_post_thumbnail('event_thumb'); } ?>
                                <figcaption class="absolute-bottom">
                                    <div class="littpaddingleft">
                                        <h2 class="whitetext nomarginbottom jubilat"><?php the_title(); ?></h2>
                                        <p><span class="whitetext thin">

                                        <?php
                                            $value = types_render_field('dato');
                                            if ( !empty($value) ) { 
                                            echo do_shortcode( '[types field="dato" format="G:i"][/types]' ); 
                                            echo " |";
                                            }
                                            else { echo ""; }
                                        ?>
                                        <?php
                                            $value = types_render_field('type');
                                            if ( !empty($value) ) { 
                                            echo $value;
                                            echo " |";
                                            }
                                            else { echo ""; }
                                        ?>
                                        <?php
                                            $value = types_render_field('stad');
                                            if ( !empty($value) ) { echo $value; }
                                            else { echo ""; }
                                        ?>

                                        </span></p>
                                    </div>
                                </figcaption>           
                            </figure>  
                        </div> 
                    </div>   
                </a> 
            </div> <!-- End col-md-4  -->                         


    <?php
        // increase our counter... 
        $x++;
        endwhile;  
    ?>

    </div> <!-- Close col-md-12 -->
    </div> <!-- close final day -->
</div> <!-- End isotope-list -->
<?php endif; ?>

遇到$curdate等问题...可在此处找到该页面:http://vekvemedia.no/oppdrag/dokfilm/ (滚动至计划)

var_dump($posts)回应了这个:

array(2) { [0]=> object(WP_Post)#130 (24) { ["ID"]=> int(26) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-01-23 14:07:20" ["post_date_gmt"]=> string(19) "2016-01-23 13:07:20" ["post_content"]=> string(56) "Verdas trivlegaste dokumentarfilmfestival fyllar 20 år!" ["post_title"]=> string(39) "20 år med Dokfilm – 27. til 30. mars" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(32) "20-ar-med-dokfilm-27-til-30-mars" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2016-01-23 14:07:20" ["post_modified_gmt"]=> string(19) "2016-01-23 13:07:20" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(42) "http://vekvemedia.no/oppdrag/dokfilm/?p=26" ["menu_order"]=> int(0) ["post_type"]=> string(4) "post" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#113 (24) { ["ID"]=> int(1) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-01-23 13:13:22" ["post_date_gmt"]=> string(19) "2016-01-23 12:13:22" ["post_content"]=> string(101) "Velkommen til WordPress. Dette er ditt første innlegg. Rediger eller slett det, og start bloggingen!" ["post_title"]=> string(12) "Hei, verden!" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(4) "open" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "hei-verden" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2016-01-23 13:13:22" ["post_modified_gmt"]=> string(19) "2016-01-23 12:13:22" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(41) "http://vekvemedia.no/oppdrag/dokfilm/?p=1" ["menu_order"]=> int(0) ["post_type"]=> string(4) "post" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "1" ["filter"]=> string(3) "raw" } }

Meta键:wpcf-dato。元值:1458741600

1 个答案:

答案 0 :(得分:1)

不幸的是,日期元查询仅适用于(假设)YYYY-mm-dd存储日期,我之前遇到过正确存储日期的问题所以最好的选择是实际存储unix日期(包含数字),所以越大数字越大日期。

但是因为您要返回所有帖子而不使用分页功能,所以您无法显示已过期的事件

这样的事情应该有效:

<?php 
// your query args...
$args = array(
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_query'        => array(
           'key' => 'dato',
           'value' => strtotime('now'),
           'compare' => '>=',
           'type'    => 'numeric',
    ),
    'orderby' => 'meta_value_num', 
    'meta_key' => 'wpcf-dato',
    'order' => 'ASC', // sort ascending, we want the nearest dates to display first?
    );


$the_query = new WP_Query($args);





$x= 0;

if ( $the_query->have_posts() ) : ?> <!-- Listing all the events -->

<div id="isotope-list">

    <!-- open the first day -->
    <div id="isotope-day">

    <?php while ( $the_query->have_posts() ) : $the_query->the_post();

        $newtime = false;
        $date= strtotime( get_post_meta($post->ID, 'dato', true) ); //get a unix date, i'm not sure of the dateformat you are using but it should be ok..if not ref strtotime for your date format
        $now = strtotime('now');

        if($date < $now){
            continue;
        }


        //do we need a new day?? yes if not the same date!
        if( !isset($curdate) || date('d/m/Y', $date) !== $curdate   ){
            $curday = date('l', $date );
            $curdate= date('d/m/Y', $date);
            $newdate= true; 
            $x++;
        }

        //do we need a new time??
        if( !isset($curtime) || date('h', $date ) != $curtime  ){
            $curtime = date('h', $date );
            $newtime= true; 
        }

    ?>

    <?php if($newday): ?>

        <?php if ($x>0):?>
            </div> <!-- End previous day when adding a new one except in first instance -->
        <?php endif; ?>

        <div id="isotope-day">
        <h1><?php echo $curday.' '.$curdate; ?></h1>

    <?php endif; ?>
        <?php if($newtime) : ?>
            <h3><?php echo $curtime; ?>:00</h3>
        <?php endif; ?>

        <!-- your event here -->
    </div> <!-- End col-md-4  -->

    <?php
        // increase our counter... 
        $x++;
        endwhile;  
    ?>

    </div> <!-- close final day -->

</div> <!-- End isotope-list -->
<?php endif; ?>