php 修改Wordpress后端中post类型的列所需的挂钩示例,包括按附加排序的功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 修改Wordpress后端中post类型的列所需的挂钩示例,包括按附加排序的功能相关的知识,希望对你有一定的参考价值。

<?php
function product_knowledge_columns($columns) {
	$columns = array(
		'cb'	 		=> '<input type="checkbox" />',
		'title' 		=> 'Title',
		'product'		=> 'Product'
	);
	return $columns;
}
add_filter('manage_edit-product-knowledge_columns', 'product_knowledge_columns' );

function product_knowledge_custom_columns($column) {
	global $post;

	if ($column == 'product') {
		$terms = get_the_terms($post->ID, 'products');
		echo $terms[0]->name;
	}
}
add_action('manage_product-knowledge_posts_custom_column', 'product_knowledge_custom_columns' );

function product_knowledge_column_register_sortable( $columns ) {
	$columns['product'] = 'product';
	return $columns;
}
add_filter('manage_edit-product-knowledge_sortable_columns', 'product_knowledge_column_register_sortable' );

function product_knowledge_sort_custom_column($clauses, $wp_query) {
    global $wpdb;
    if (isset($wp_query->query['orderby']) && $wp_query->query['orderby'] == 'product') {
        $clauses['join'] .= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
        $clauses['where'] .= "AND (taxonomy = 'products' OR taxonomy IS NULL)";
        $clauses['groupby'] = "object_id";
        $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY {$wpdb->terms}.name, {$wpdb->posts}.menu_order ASC)";
        if(strtoupper($wp_query->get('order')) == 'ASC'){
            $clauses['orderby'] .= 'ASC';
        } else{
            $clauses['orderby'] .= 'DESC';
        }
    }
    return $clauses;
}
add_filter('posts_clauses', 'product_knowledge_sort_custom_column', 10, 2);

?>

以上是关于php 修改Wordpress后端中post类型的列所需的挂钩示例,包括按附加排序的功能的主要内容,如果未能解决你的问题,请参考以下文章

php Wordpress中的核心帖子类型[wp-includes / post.php]

PHP Wordpress自定义帖子类型:帖子网址中的POST ID

wordpress进阶教程:wordpress文章类型

为自定义帖子类型创建存档:wordpress

wordpress调用指定类型post_type的文章

WordPress公文包自定义Post类型筛选