wordpress 自定义 后台 post 字段显示

原文地址:http://www.smashingmagazine.com/2013/12/05/modifying-admin-post-lists-in-wordpress/

google翻译成中文:http://translate.google.com.hk/translate?hl=zh-CN&sl=en&u=http://www.smashingmagazine.com/2013/12/05/modifying-admin-post-lists-in-wordpress/&prev=/search%3Fq%3Dadmin%2Bpost%2Bfilter%26newwindow%3D1%26safe%3Dstrict%26espv%3D2%26biw%3D1920%26bih%3D940


这里贴出自己制作功能应用时用到的代码:

/*Adding Custom Table Headers
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------*/
add_filter('manage_post_posts_columns', 'bs_expire_table_head');
function bs_expire_table_head( $defaults ) {
    $defaults['expire_date']  = 'Expire Date';
    return $defaults;
}
/*Fill 'er Up!
--------------------------------------------------------------------------*/
add_action( 'manage_post_posts_custom_column', 'bs_expire_table_content', 10, 2 );

function bs_expire_table_content( $column_name, $post_id ) {
    if ($column_name == 'expire_date') {
    $expire_date = get_post_meta( $post_id, 'custom_expire', true );
      echo  $expire_date ;
    }
}
/*Ordering Columns
--------------------------------------------------------------------------*/
add_filter( 'manage_edit-post_sortable_columns', 'bs_expire_table_sorting' );
function bs_expire_table_sorting( $columns ) {
  $columns['expire_date'] = 'expire_date';
  return $columns;
}

add_filter( 'request', 'bs_expire_date_column_orderby' );
function bs_expire_date_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'expire_date' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'custom_expire',
            'orderby' => 'meta_value'
        ) );
    }

    return $vars;
}

/*Data Filtering
--------------------------------------------------------------------------*/
add_action( 'restrict_manage_posts', 'bs_expire_table_filtering' );
function bs_expire_table_filtering() {

  global $wpdb;
  if ( 1/*$screen->post_type == 'post'*/ ) {
	$selected = ( !empty( $_GET['expire_date'] ) AND $_GET['expire_date'] == 'Expired' ) ? 'selected="select"' : '';
	echo '';
  }
}
/*we need to add rules to the query
--------------------------------------------------------------------------*/
add_filter( 'parse_query','bs_expire_table_filter' );
function bs_expire_table_filter( $query ) {
	//var_dump($query);
  if( is_admin() AND $query->query['post_type'] == 'post' ) {		  
    $qv = &$query->query_vars;
	//echo "
".print_r($query,true)."
"; $qv['meta_query'] = array(); if( !empty( $_GET['expire_date'] ) && $_GET['expire_date'] == 'Expired' ) { add_filter( 'posts_where' , 'posts_where_statement' ); function posts_where_statement( $where ) { global $wpdb; $where .= "AND ID IN( SELECT post_id from $wpdb->postmeta where meta_key = 'custom_expire' AND meta_value <>'' AND STR_TO_DATE(meta_value,'%m/%d/%Y') < CURDATE() )"; return $where; } } if( !empty( $_GET['orderby'] ) AND $_GET['orderby'] == 'expire_date' ) { $qv['orderby'] = 'meta_value'; $qv['meta_key'] = 'custom_expire'; $qv['order'] = strtoupper( $_GET['order'] ); } } }


你可能感兴趣的:(wordpress,PHP)