Wordpress存档按发布日期排序事件,而不是事件日期

时间:2013-11-20 00:21:55

标签: php wordpress events post

我已经在这个问题上与WordPress争夺了好几个星期,并且无法理解。也许你们其中一个人可以帮助我...

我正在使用WordPress来管理加州大学的部门网站,并且在涉及到事件时我一直遇到同样的问题。基本上我想要一个页面存档所有活动帖子按事件日期(在帖子本身内设置),但它会一直按帖子日期发布档案。进入存档的链接结构如下:

http://www.csulb.edu/colleges/cla/news_events/?category_name=events

因此,它指定存档只显示带有类别事件的帖子,但它没有按照我想要的顺序显示它们。

有没有人有任何想法?我可以根据要求提供代码。谢谢!

2 个答案:

答案 0 :(得分:2)

我有类似的问题。具有议程的网站,其中事件日期由ACF存储为元信息。实际上它被保存为时间戳值(不是MySQL日期类型),因为它是一个DateTime Picker字段。

我做了以下修改:

  1. 创建wp_get_archives的修改副本以管理议程 档案计数。我的新函数wp_get_archives_mdv位于 的functions.php。修改SQL查询以包含到的连接 wp_postmeta表,只有过去的事件。
  2. 我制作了过滤器getarchives_where和getarchives_join的副本 从wp_get_archives_mdv中调用whic(实际上这可能是 通过对SQL语句进行硬编码来避免,但它似乎更清晰 我)
  3. 创建get_the_date的修改副本以管理存档事件 从archive.php。
  4. 调用的日期
  5. 最后,迷上pre_get_posts来调整发出的查询 archives.php loop。
  6. 我提供代码以防万一有人感兴趣。

    <强> wp_get_archives

    /**
     * Display archive links based on type and format.
     * for events of MDV. Adapted from WP 4.4 wp-includes/general-template.php
     * @return string|void String when retrieving.
     */
    function wp_get_archives_mdv( $args = '' ) {
        global $wpdb, $wp_locale;
    
        $defaults = array(
            'type' => 'monthly', 'limit' => '',
            'format' => 'html', 'before' => '',
            'after' => '', 'show_post_count' => false,
            'echo' => 1, 'order' => 'DESC',
            'post_type' => 'post'
        );
    
        $r = wp_parse_args( $args, $defaults );
    
        $post_type_object = get_post_type_object( $r['post_type'] );
        if ( ! is_post_type_viewable( $post_type_object ) ) {
            return;
        }
        $r['post_type'] = $post_type_object->name;
    
        if ( '' == $r['type'] ) {
            $r['type'] = 'monthly';
        }
    
        if ( ! empty( $r['limit'] ) ) {
            $r['limit'] = absint( $r['limit'] );
            $r['limit'] = ' LIMIT ' . $r['limit'];
        }
    
        $order = strtoupper( $r['order'] );
        if ( $order !== 'ASC' ) {
            $order = 'DESC';
        }
    
        // this is what will separate dates on weekly archive links
        $archive_week_separator = '&#8211;';
    
        // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
        $archive_date_format_over_ride = 0;
    
        // options for daily archive (only if you over-ride the general date format)
        $archive_day_date_format = 'Y/m/d';
    
        // options for weekly archive (only if you over-ride the general date format)
        $archive_week_start_date_format = 'Y/m/d';
        $archive_week_end_date_format   = 'Y/m/d';
    
        if ( ! $archive_date_format_over_ride ) {
            $archive_day_date_format = get_option( 'date_format' );
            $archive_week_start_date_format = get_option( 'date_format' );
            $archive_week_end_date_format = get_option( 'date_format' );
        }
    
        $sql_where = $wpdb->prepare( "WHERE post_type = %s AND post_status = 'publish'", $r['post_type'] );
    
        /**
         * Filter the SQL WHERE clause for retrieving archives.
         *
         * @since 2.2.0
         *
         * @param string $sql_where Portion of SQL query containing the WHERE clause.
         * @param array  $r         An array of default arguments.
         */
        $where = apply_filters( 'getarchives_events_mdv_where', $sql_where, $r ,$args);
    
        /**
         * Filter the SQL JOIN clause for retrieving archives.
         *
         * @since 2.2.0
         *
         * @param string $sql_join Portion of SQL query containing JOIN clause.
         * @param array  $r        An array of default arguments.
         */
        $join = apply_filters( 'getarchives_events_mdv_join', '', $r );
    
        $output = '';
    
        $last_changed = wp_cache_get( 'last_changed', 'posts' );
        if ( ! $last_changed ) {
            $last_changed = microtime();
            wp_cache_set( 'last_changed', $last_changed, 'posts' );
        }
    
        $limit = $r['limit'];
    
        if ( 'monthly' == $r['type'] ) {        
            $query = "SELECT YEAR(FROM_UNIXTIME(wp_postmeta.meta_value)) AS `year`, MONTH(FROM_UNIXTIME(wp_postmeta.meta_value)) AS `month`, count(ID) as posts";
            $query = $query . " FROM $wpdb->posts $join";
            $query = $query . $where . " GROUP BY YEAR(FROM_UNIXTIME(wp_postmeta.meta_value)), MONTH(FROM_UNIXTIME(wp_postmeta.meta_value))" ;
            $query = $query . "ORDER BY wp_postmeta.meta_value $order $limit"; 
    
            $key = md5( $query );
            $key = "wp_get_archives_mdv:$key:$last_changed";
            if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
                $results = $wpdb->get_results( $query );
                wp_cache_set( $key, $results, 'posts' );
            }
    
            if ( $results ) {
                $after = $r['after'];
                foreach ( (array) $results as $result ) {
                    $url = get_month_link( $result->year, $result->month );
                    if ( 'post' !== $r['post_type'] ) {
                        $url = add_query_arg( 'post_type', $r['post_type'], $url );
                    }
                    /* translators: 1: month name, 2: 4-digit year */
                    $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year );
                    if ( $r['show_post_count'] ) {
                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
                    }
                    $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
                }
            }
        } elseif ( 'yearly' == $r['type'] ) {
            $query = "SELECT YEAR(FROM_UNIXTIME(wp_postmeta.meta_value)) AS `year`, count(ID) as posts";
            $query = $query . " FROM $wpdb->posts $join";
            $query = $query . $where . " GROUP BY YEAR(FROM_UNIXTIME(wp_postmeta.meta_value))" ;
            $query = $query . "ORDER BY wp_postmeta.meta_value $order $limit"; 
    
    
            $key = md5( $query );
            $key = "wp_get_archives_mdv:$key:$last_changed";
            if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
                $results = $wpdb->get_results( $query );
                wp_cache_set( $key, $results, 'posts' );
            }
            if ( $results ) {
                $after = $r['after'];
                foreach ( (array) $results as $result) {
                    $url = get_year_link( $result->year );
                    if ( 'post' !== $r['post_type'] ) {
                        $url = add_query_arg( 'post_type', $r['post_type'], $url );
                    }
                    $text = sprintf( '%d', $result->year );
                    if ( $r['show_post_count'] ) {
                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
                    }
                    $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
                }
            }
        } elseif ( 'daily' == $r['type'] ) {
            $query = "SELECT YEAR(FROM_UNIXTIME(wp_postmeta.meta_value)) AS `year`, MONTH(FROM_UNIXTIME(wp_postmeta.meta_value)) AS `month`,DAYOFMONTH(FROM_UNIXTIME(wp_postmeta.meta_value)) AS `dayofmonth`,  count(ID) as posts";
            $query = $query . " FROM $wpdb->posts $join";
            $query = $query . $where . " GROUP BY YEAR(FROM_UNIXTIME(wp_postmeta.meta_value)), MONTH(FROM_UNIXTIME(wp_postmeta.meta_value)),DAYOFMONTH(FROM_UNIXTIME(wp_postmeta.meta_value))" ;
            $query = $query . "ORDER BY wp_postmeta.meta_value $order $limit";
    
            $key = md5( $query );
            $key = "wp_get_archives_mdv:$key:$last_changed";
            if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
                $results = $wpdb->get_results( $query );
                wp_cache_set( $key, $results, 'posts' );
            }
            if ( $results ) {
                $after = $r['after'];
                foreach ( (array) $results as $result ) {
                    $url  = get_day_link( $result->year, $result->month, $result->dayofmonth );
                    if ( 'post' !== $r['post_type'] ) {
                        $url = add_query_arg( 'post_type', $r['post_type'], $url );
                    }
                    $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth );
                    $text = mysql2date( $archive_day_date_format, $date );
                    if ( $r['show_post_count'] ) {
                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
                    }
                    $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
                }
            }
        }  
    
        elseif ( 'weekly' == $r['type'] ) {
            $week = _wp_mysql_week("FROM_UNIXTIME(`meta_value`)");
            $query = "SELECT DISTINCT $week AS `week`, year(FROM_UNIXTIME(wp_postmeta.meta_value) ) AS `year`, count(ID) as posts";
            $query = $query . " FROM $wpdb->posts $join";
            $query = $query . $where . " GROUP BY $week, year(FROM_UNIXTIME(wp_postmeta.meta_value))" ;
            $query = $query . "ORDER BY wp_postmeta.meta_value $order $limit";
    
            $key = md5( $query );
            $key = "wp_get_archives_mdv:$key:$last_changed";
            if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
                $results = $wpdb->get_results( $query );
                wp_cache_set( $key, $results, 'posts' );
            }
            $arc_w_last = '';
            if ( $results ) {
                $fechai  = new DateTime();
                $fechaf  = new DateTime();
                $after = $r['after'];
                foreach ( (array) $results as $result ) {
                    $fechai ->setISODate($result->year, $result->week);
                    $fechai->setTime(0,0,0);
                    $fechaf ->setISODate($result->year, $result->week); 
                    $fechaf->setTime(0,0,0);
                    $fechaf->modify('+7 day');
    
                    $arc_year       = $result->year;
                    $arc_w_last     = $result->week;
    
                    $arc_week_start = date_i18n( $archive_week_start_date_format, $fechai->getTimestamp () );
                    $arc_week_end   = date_i18n( $archive_week_end_date_format,$fechaf->getTimestamp ());
                    $url            = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&amp;', '=', $result->week );
                    if ( 'post' !== $r['post_type'] ) {
                        $url = add_query_arg( 'post_type', $r['post_type'], $url );
                    }
                    $text           = $arc_week_start . $archive_week_separator . $arc_week_end;
                    if ( $r['show_post_count'] ) {
                        $r['after'] = '&nbsp;(' . $result->posts . ')' . $after;
                    }
                    $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
    
                }
            }
        }    
        elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) {
            $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'data DESC, ID DESC ';
            $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit";
            $key = md5( $query );
            $key = "wp_get_archives_mdv:$key:$last_changed";
            if ( ! $results = wp_cache_get( $key, 'posts' ) ) {
                $results = $wpdb->get_results( $query );
                wp_cache_set( $key, $results, 'posts' );
            } 
            if ( $results ) {
                foreach ( (array) $results as $result ) {
                    if ( $result->data != '0000-00-00 00:00:00' ) {
                        $url = get_permalink( $result );
                        if ( $result->post_title ) {
                            /** This filter is documented in wp-includes/post-template.php */
                            $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
                        } else {
                            $text = $result->ID;
                        }
                        $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
                    }
                }
            }
        } 
    
        if ( $r['echo'] ) {
            echo $output;
        } else {
            return $output;
        }
    }
    

    getarchives_where和getarchives_join

    function my_archives_join_filter($join) {
        global $wpdb;
        return $join . "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)";
    }
    add_filter('getarchives_events_mdv_join', 'my_archives_join_filter');
    
    
    function my_archives_where_filter($where,$args) {
        global $wpdb;
        $data = current_time( 'timestamp');
        $post_type  = isset($args['post_type'])  ? $args['post_type']  : 'agenda';          
        $where  = "WHERE $wpdb->posts.post_type = '$post_type' AND $wpdb->posts.post_status = 'publish' AND $wpdb->postmeta.meta_key ='data' AND $wpdb->postmeta.meta_value <='$data'";
        return $where  ;
    }
    add_filter('getarchives_events_mdv_where', 'my_archives_where_filter',10,3);
    

    get_the_date

    function get_the_date_event_mdv ( $d = '', $post = null ) {
        $post = get_post( $post );
    
        if ( ! $post ) {
            return false;
        }
        if ( '' == $d ) {
            //$the_date = mysql2date( get_option( 'date_format' ), get_field('data') );
            $the_date = date_i18n (get_option( 'date_format' ),get_field('data'));
        } else {
            //$the_date = mysql2date( $d, get_field('data') );
            $the_date = date_i18n ($d,get_field('data'));
        }
    
        return $the_date;
        // This filter is not implemented i MDV modification. return apply_filters( 'get_the_date', $the_date, $d, $post );
    }
    

    pre_get_posts hook

    function my_pre_get_posts( $query ) {
    
        // exit out if it's the admin or it isn't the main query 
        if (is_admin() || ! $query->is_main_query())
        {
            return $query;
        }
    
        if ( !is_admin() && $query->is_main_query() ) {
          if ($query->is_search) {
            $query->set('post_type', array( 'post', 'agenda', 'page' ,'professorat'));
          }
        }
    
        if ( is_post_type_archive( 'agenda' ) ) 
            {
                // Set the posts_per_page to 9 = 3*num_agenda_linia
                $query->set( 'posts_per_page', 3*num_agenda_linia);     
    
    
                if (is_date()) 
                    { 
    
                    if ($query->query['w']!='' ) 
                        {                   
                        if ($query->query['year']=='')      
                            {$year=date("Y");}
                        else                                
                            {$year=$query->query['year'];}
    
                        $fechai = new DateTime();
                        $fechai->setISODate($year, $query->query['w']);
                        $fechai->setTime(0,0,0);
    
                        $fechaf = new DateTime();                   
                        $fechaf->setISODate($year, $query->query['w']);
                        $fechaf->setTime(0,0,0);
                        $fechaf->modify('+7 day');
    
    
                        $meta_query = array(
                            'relation' => 'AND',
                            data_clause1=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  current_time( 'timestamp')  
                            ) ,
                            data_clause2=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '>=',
                            'value'     =>  $fechai->getTimestamp ()
                            ),
                            data_clause3=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  $fechaf->getTimestamp ()
                            )                   
                            );                      
    
                         $query->set('meta_query',$meta_query); 
    
                        $query->set( 'order' , 'asc' );
                        $query->set( 'meta_key', 'data');
                        $query->set( 'orderby', 'meta_value');  
    
                        unset($query->query_vars['year']);
                        unset($query->query_vars['monthnum']);
                        unset($query->query_vars['day']);
                        unset($query->query_vars['w']);                 
                        }               
                    elseif  (is_month())
                        {         
                        $meta_query = array(
                            'relation' => 'AND',
                            data_clause1=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  current_time( 'timestamp')  
                            ) ,
                            data_clause2=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '>=',
                            'value'     =>  mktime(0,0,0,$query->query['monthnum'],1,$query->query['year']) 
                            ),
                            data_clause3=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  mktime(0,0,0,$query->query['monthnum']+1,1,$query->query['year'])   
                            )                   
                            );                  
                        $query->set('meta_query',$meta_query);  
    
                        unset($query->query_vars['year']);
                        unset($query->query_vars['monthnum']);
                        unset($query->query_vars['day']);
                        unset($query->query_vars['w']);          
                        }                   
                    elseif (is_year())
                        {   
                        $meta_query = array(
                            'relation' => 'AND',
                            data_clause1=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  current_time( 'timestamp')  
                            ) ,
                            data_clause2=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '>=',
                            'value'     =>  mktime(0,0,0,1,1,$query->query['year']) 
                            ),
                            data_clause3=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  mktime(0,0,0,1,1,$query->query['year']+1)   
                            )                   
                            );                  
                        $query->set('meta_query',$meta_query);  
    
                        $query->set( 'order' , 'desc' );
                        $query->set( 'meta_key', 'data');
                        $query->set( 'orderby', 'meta_value');    
                        unset($query->query_vars['year']);
                        unset($query->query_vars['monthnum']);
                        unset($query->query_vars['day']);
                        unset($query->query_vars['w']); 
                    }
                    elseif (is_day())
                    {                                        
                        $meta_query = array(
                            'relation' => 'AND',
                            data_clause1=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  current_time( 'timestamp')  
                            ) ,
                            data_clause2=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '>=',
                            'value'     =>  mktime(0,0,0,$query->query['monthnum'],$query->query['day'],$query->query['year'])  
                            ),
                            data_clause3=> array(
                            'key'       => 'data',
                            'type'      => 'CHAR',
                            'compare'   => '<',
                            'value'     =>  (mktime(0,0,0,$query->query['monthnum'],$query->query['day']+1,$query->query['year']))
                            )                   
                            );                  
                        $query->set('meta_query',$meta_query);  
    
                        $query->set( 'order' , 'asc' );
                        $query->set( 'meta_key', 'data');
                        $query->set( 'orderby', 'meta_value');  
    
                        unset($query->query_vars['year']);
                        unset($query->query_vars['monthnum']);
                        unset($query->query_vars['day']);
                        unset($query->query_vars['w']); 
                    }   
    
                    elseif (is_category() && $evolve_category_page_title) 
                    {} 
                    elseif (is_tag()) 
                    {}
    
                    }
                else
                    {    
                    $meta_query = array(
                        data_clause=> array(
                        'key'       => 'data',
                        'type'      => 'CHAR',
                        'compare'   => '>=',
                        'value'     =>  current_time( 'timestamp')
                        ) 
                        );                  
                    $query->set('meta_query',$meta_query);  
    
                    $query->set( 'order' , 'asc' );
                    $query->set( 'meta_key', 'data');
                    $query->set( 'orderby', 'meta_value');      
    
                    }               
            }
    
    
      return $query;
    }
    

    再见

    Zulok

答案 1 :(得分:0)

如何处理和存储EVENT DATE?

我会使用添加的过滤器查询EVENT DATE并传递排序顺序

Function query_posts

Codex:http://codex.wordpress.org/Function_Reference/query_posts

如何对archive.php进行排序

add_action( 'pre_get_posts', 'my_change_sort_order'); 
    function my_change_sort_order($query){
        if(is_archive()):
         //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
           //Set the order ASC or DESC
           $query->set( 'order', 'ASC' );
           //Set the orderby
           $query->set( 'orderby', 'title' );
        endif;    
    };

从这里复制的答案:https://wordpress.stackexchange.com/questions/39817/sort-results-by-name-asc-order-on-archive-php

如果插件可以接受,您也可以尝试这样做: http://wordpress.org/plugins/archive-posts-sort-customize/

(不隶属于链接)

将来,您应该考虑发布到http://wordpress.stackoverflow.com