Wordpress管理可排序列使用两个自定义字段

时间:2016-05-05 05:41:59

标签: php mysql wordpress

我正在编写Wordpress插件,以便在管理员中添加可排序列。该列需要根据两个自定义字段按日期排序 - 一个完整的月份名称(八月,九月等)和一个四位数的年份。

我一直关注this excellent and extremely well commented tutorial,但我的SQL技能几乎为零,我不知道如何在月份字段之后将年份字段加入查询。如果我只使用一个包含月份和年份的字段,那么我可以完成所有工作,但这不是网站的设置方式。

如何加入第二个字段,以格式%M%Y或%M%Y格式显示结果,以便将其转换为日期?

代码的相关部分:

    switch( $orderby ) {

        // If we're ordering by period
        case 'period':

            /* We have to join the postmeta table to include our date in the query. */
            $pieces[ 'join' ] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'month'";

            // Then tell the query to order by our date
            $pieces[ 'orderby' ] = "STR_TO_DATE( wp_rd.meta_value,'%M %Y' ) $order, " . $pieces[ 'orderby' ];

完整代码:

<?php

    // Register the column
    function month_column_register( $columns ) {

        unset( $columns['tags'], $columns['comments'] ); // Gets rid of these columns

        $new_columns = array();
             foreach( $columns as $key => $value ) {
                $new_columns[ $key ] = $value;
                if ( $key == 'title' )
                   $new_columns[ 'month_column' ] = 'Period'; //Putting the month column after the title column
             }

             return $new_columns;

        return $columns;
    }

    add_filter( 'manage_posts_columns', 'month_column_register' );



    // Display the column content
    function month_column_display( $column_name, $post_id ) {
        if ( 'month_column' != $column_name )
            return;

        $month = get_post_meta($post_id, 'month', true);
        $year = get_post_meta($post_id, 'year', true);

        echo $month . ' ' . $year;
    }

    add_action( 'manage_posts_custom_column', 'month_column_display', 10, 2 );




    function register_sortable ( $columns ) 
    {
        $columns['month_column'] = 'period';
        return $columns;
    }
    add_filter('manage_edit-post_sortable_columns', 'register_sortable');



    add_filter( 'posts_clauses', 'manage_wp_posts_be_qe_posts_clauses', 1, 2 );

    function manage_wp_posts_be_qe_posts_clauses( $pieces, $query ) {
        global $wpdb;

        /* We only want our code to run in the main WP query AND if an orderby query variable is designated. */
        if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {

            // Get the order query variable - ASC or DESC
            $order = strtoupper( $query->get( 'order' ) );

            // Make sure the order setting qualifies. If not, set default as ASC
            if ( ! in_array( $order, array( 'ASC', 'DESC' ) ) )
                $order = 'ASC';

            switch( $orderby ) {

                // If we're ordering by period
                case 'period':

                    /* We have to join the postmeta table to include our date in the query. */
                    $pieces[ 'join' ] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'month'";

                    // Then tell the query to order by our date
                    $pieces[ 'orderby' ] = "STR_TO_DATE( wp_rd.meta_value,'%M %Y' ) $order, " . $pieces[ 'orderby' ];

                    break;

            }

        }
        return $pieces;
    }

    ?>

0 个答案:

没有答案