Skip to content

Commit 41788ce

Browse files
committed
快速编辑自定义排序
增加:后台网址列表增加分类筛选功能 增加:后台网址列表自定义排序栏,并且支持快速编辑自定义排序数值 增加:后台网址分类列表自定义排序栏,并且支持快速编辑自定义排序数值 修复:站内搜索会显示一些错误结果的bug
1 parent d9b7d97 commit 41788ce

File tree

4 files changed

+296
-35
lines changed

4 files changed

+296
-35
lines changed

inc/inc.php

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function search_filter_page($query) {
122122
}
123123
return $query;
124124
}
125-
add_filter('pre_get_posts','search_filter_page');
125+
//add_filter('pre_get_posts','search_filter_page');
126126

127127
/**
128128
* 去除wordpress前台顶部工具条
@@ -480,12 +480,14 @@ function include_post_types_in_search($query) {
480480
}
481481
return $query;
482482
}
483-
add_action('pre_get_posts', 'include_post_types_in_search');
483+
//add_action('pre_get_posts', 'include_post_types_in_search');
484+
484485

485486
/**
486487
* 让搜索支持自定义字段
487488
*/
488-
add_action('posts_search', function($search, $query){
489+
//add_action('posts_search', 'include_custom_fields_in_search',2,2);
490+
function include_custom_fields_in_search ($search, $query){
489491
global $wpdb;
490492

491493
if ($query->is_main_query() && !empty($query->query['s'])) {
@@ -496,7 +498,7 @@ function include_post_types_in_search($query) {
496498
$search .= $wpdb->prepare($sql, $like);
497499
}
498500
return $search;
499-
},2,2);
501+
};
500502

501503
function format_url($url){
502504
if($url == '')
@@ -511,33 +513,47 @@ function format_url($url){
511513
}
512514
}
513515

514-
// 分页代码
515-
function pagination($query_string){
516-
global $posts_per_page, $paged;
517-
$my_query = new WP_Query($query_string ."&posts_per_page=-1");
518-
$total_posts = $my_query->post_count;
519-
if(empty($paged))$paged = 1;
520-
$prev = $paged - 1;
521-
$next = $paged + 1;
522-
$range = 2; // only edit this if you want to show more page-links
523-
$showitems = ($range * 2)+1;
524-
525-
$pages = ceil($total_posts/$posts_per_page);
526-
if(1 != $pages){
527-
echo "<div class='pagination'>";
528-
echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<a href='".get_pagenum_link(1)."'>最前</a>":"";
529-
echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."'>上一页</a>":"";
530-
531-
for ($i=1; $i <= $pages; $i++){
532-
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
533-
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
516+
517+
/**
518+
* 修改搜索查询的sql代码,将postmeta表左链接进去。
519+
*/
520+
add_filter('posts_join', 'cf_search_join' );
521+
function cf_search_join( $join ) {
522+
if(is_admin())
523+
return $join;
524+
global $wpdb;
525+
if ( is_search() ) {
526+
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
534527
}
535-
}
536-
537-
echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."'>下一页</a>" :"";
538-
echo ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."'>最后</a>":"";
539-
echo "</div>\n";
528+
return $join;
529+
}
530+
531+
/**
532+
* 在wordpress查询代码中加入自定义字段值的查询。
533+
*/
534+
add_filter('posts_where', 'cf_search_where');
535+
function cf_search_where( $where ) {
536+
if(is_admin())
537+
return $where;
538+
global $pagenow, $wpdb;
539+
if ( is_search() ) {
540+
$where = preg_replace("/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
541+
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
540542
}
543+
return $where;
544+
}
545+
546+
/**
547+
* 去重
548+
*/
549+
add_filter ('posts_distinct', 'cf_search_distinct');
550+
function cf_search_distinct($where) {
551+
if(is_admin())
552+
return $where;
553+
global $wpdb;
554+
if ( is_search() ) {
555+
return "DISTINCT";
541556
}
557+
return $where;
558+
}
542559

543-

inc/meta-boxes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ function save_sites_postdata($post_id) {
121121

122122
add_action( 'admin_footer', 'add_script_and_styles' );
123123
function add_script_and_styles() {
124+
// 增加位置判断
125+
$current_screen = get_current_screen();
126+
if (!is_object($current_screen) || ($current_screen->id != 'sites') || ($current_screen->post_type != 'sites')) return;
124127
echo "<script>
125128
jQuery(document).ready(function(){
126129
var dami_upload_frame;

inc/post-type.php

Lines changed: 246 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ function term_order_field( $term, $taxonomy ) {
124124
add_action('created_favorites','save_term_order',10,1);
125125
add_action('edit_favorites','save_term_order',10,1);
126126
function save_term_order( $term_id ) {
127-
update_term_meta( $term_id, '_term_order', $_POST[ '_term_order' ] );
127+
if (isset($_POST['_term_order'])) {
128+
update_term_meta( $term_id, '_term_order', $_POST[ '_term_order' ] );
129+
}
130+
$ca_menu_id = esc_attr($_POST['ca_ordinal']);
131+
if ($ca_menu_id)
132+
update_term_meta( $term_id, '_term_order', $ca_menu_id);
128133
}
129134

130135

@@ -145,10 +150,247 @@ function custom_sites_rewrites_init(){
145150
add_rewrite_rule(
146151
'sites/([0-9]+)?.html$',
147152
'index.php?post_type=sites&p=$matches[1]',
148-
'top' );
153+
'top'
154+
);
149155
add_rewrite_rule(
150156
'sites/([0-9]+)?.html/comment-page-([0-9]{1,})$',
151157
'index.php?post_type=sites&p=$matches[1]&cpage=$matches[2]',
152158
'top'
153-
);
154-
}
159+
);
160+
}
161+
162+
163+
//此部分功能是生成分类下拉菜单
164+
add_action('restrict_manage_posts','io_post_type_filter',10,2);
165+
function io_post_type_filter($post_type, $which){
166+
if('sites' !== $post_type){ //这里为自定义文章类型,需修改
167+
return; //检查是否是我们需要的文章类型
168+
}
169+
$taxonomy_slug = 'favorites'; //这里为自定义分类法,需修改
170+
$taxonomy = get_taxonomy($taxonomy_slug);
171+
$selected = '';
172+
$request_attr = 'favorites'; //这里为自定义分类法,需修改
173+
if ( isset($_REQUEST[$request_attr] ) ) {
174+
$selected = $_REQUEST[$request_attr];
175+
}
176+
wp_dropdown_categories(array(
177+
'show_option_all' => __("所有{$taxonomy->label}"),
178+
'taxonomy' => $taxonomy_slug,
179+
'name' => $request_attr,
180+
'orderby' => 'name',
181+
'selected' => $selected,
182+
'hierarchical' => true,
183+
'depth' => 5,
184+
'show_count' => true, // Show number of post in parent term
185+
'hide_empty' => false, // Don't show posts w/o terms
186+
));
187+
}
188+
//此部分功能是列出指定分类下的所有文章
189+
add_filter('parse_query','io_work_convert_restrict');
190+
function io_work_convert_restrict($query) {
191+
global $pagenow;
192+
global $typenow;
193+
if ($pagenow=='edit.php') {
194+
$filters = get_object_taxonomies($typenow);
195+
foreach ($filters as $tax_slug) {
196+
$var = &$query->query_vars[$tax_slug];
197+
if ( isset($var) && $var>0) {
198+
$term = get_term_by('id',$var,$tax_slug);
199+
$var = $term->slug;
200+
}
201+
}
202+
}
203+
return $query;
204+
}
205+
206+
/**
207+
* 文章列表添加自定义字段
208+
* https://www.iowen.cn/wordpress-quick-edit
209+
*/
210+
add_filter('manage_edit-sites_columns', 'io_ordinal_manage_posts_columns');
211+
add_action('manage_posts_custom_column','io_ordinal_manage_posts_custom_column',10,2);
212+
function io_ordinal_manage_posts_columns($columns){
213+
$columns['ordinal'] = '排序';
214+
$columns['visible'] = '可见性';
215+
return $columns;
216+
}
217+
function io_ordinal_manage_posts_custom_column($column_name,$id){
218+
switch( $column_name ) :
219+
case 'ordinal': {
220+
echo get_post_meta($id, '_sites_order', true);
221+
break;
222+
}
223+
case 'visible': {
224+
echo get_post_meta($id, '_visible', true)? "管理员" : "所有人";
225+
break;
226+
}
227+
endswitch;
228+
}
229+
230+
//分类列表添加自定义字段
231+
add_filter('manage_edit-favorites_columns', 'io_id_manage_tags_columns');
232+
add_action('manage_favorites_custom_column','io_id_manage_tags_custom_column',10,3);
233+
function io_id_manage_tags_columns($columns){
234+
$columns['ca_ordinal'] = '菜单排序';
235+
$columns['id'] = 'ID';
236+
return $columns;
237+
}
238+
function io_id_manage_tags_custom_column($null,$column_name,$id){
239+
if ($column_name == 'ca_ordinal') {
240+
echo get_term_meta($id, '_term_order', true);
241+
}
242+
if ($column_name == 'id') {
243+
echo $id;
244+
}
245+
}
246+
247+
/**
248+
* 文章列表添加自定义字段
249+
*
250+
*/
251+
add_action( 'admin_head', 'io_custom_css' );
252+
function io_custom_css(){
253+
echo '<style>
254+
#ordinal{
255+
width:80px;
256+
}
257+
</style>';
258+
}
259+
260+
//文章列表添加排序规则
261+
add_filter('manage_edit-sites_sortable_columns', 'sort_sites_order_column');
262+
//add_filter('manage_edit-favorites_sortable_columns', 'sort_favorites_order_column');
263+
add_action('pre_get_posts', 'sort_sites_order');
264+
function sort_sites_order_column($defaults)
265+
{
266+
$defaults['ordinal'] = 'ordinal';
267+
return $defaults;
268+
}
269+
function sort_favorites_order_column($defaults)
270+
{
271+
$defaults['ca_ordinal'] = 'ca_ordinal';
272+
return $defaults;
273+
}
274+
function sort_sites_order($query) {
275+
if(!is_admin())
276+
return;
277+
$orderby = $query->get('orderby');
278+
if('ordinal' == $orderby) {
279+
$query->set('meta_key', '_sites_order');
280+
$query->set('orderby', 'meta_value_num');
281+
}
282+
if('ca_ordinal' == $orderby) {
283+
$query->set('meta_key', '_term_order');
284+
$query->set('orderby', 'meta_value_num');
285+
}
286+
}
287+
288+
289+
add_action('quick_edit_custom_box', 'io_add_quick_edit', 10, 2);
290+
function io_add_quick_edit($column_name, $post_type) {
291+
if ($column_name == 'ordinal') {
292+
//请注意:<fieldset>类可以是:
293+
//inline-edit-col-left,inline-edit-col-center,inline-edit-col-right
294+
//所有列均为float:left,
295+
//因此,如果要在左列,请使用clear:both元素
296+
echo '
297+
<fieldset class="inline-edit-col-left" style="clear: both;">
298+
<div class="inline-edit-col">
299+
<label class="alignleft">
300+
<span class="title">排序</span>
301+
<span class="input-text-wrap"><input type="number" name="ordinal" class="ptitle" value=""></span>
302+
</label>
303+
<em class="alignleft inline-edit-or"> 越大越靠前</em>
304+
</div>
305+
</fieldset>';
306+
}
307+
if ($column_name == 'ca_ordinal') {
308+
echo '
309+
<fieldset>
310+
<div class="inline-edit-col">
311+
<label class="alignleft">
312+
<span class="title">排序</span>
313+
<span class="input-text-wrap"><input type="number" name="ca_ordinal" class="ptitle" value=""></span>
314+
</label>
315+
<em class="alignleft inline-edit-or"> 越大越靠前</em>
316+
</div>
317+
</fieldset>';
318+
}
319+
}
320+
321+
322+
//保存和更新数据
323+
add_action('save_post', 'io_save_quick_edit_data');
324+
function io_save_quick_edit_data($post_id) {
325+
//如果是自动保存日志,并非我们所提交数据,那就不处理
326+
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
327+
return $post_id;
328+
// 验证权限,'sites' 为文章类型,默认为 'post' ,这里为我自定义的文章类型'sites'
329+
if ( 'sites' == $_POST['post_type'] ) {
330+
if ( !current_user_can( 'edit_page', $post_id ) )
331+
return $post_id;
332+
} else {
333+
if ( !current_user_can( 'edit_post', $post_id ) )
334+
return $post_id;
335+
}
336+
$post = get_post($post_id);
337+
// 'ordinal' 与前方代码对应
338+
if (isset($_POST['ordinal']) && ($post->post_type != 'revision')) {
339+
$left_menu_id = esc_attr($_POST['ordinal']);
340+
if ($left_menu_id)
341+
update_post_meta( $post_id, '_sites_order', $left_menu_id);// ‘_sites_order’为自定义字段
342+
}
343+
}
344+
345+
//输出js
346+
add_action('admin_footer', 'ashuwp_quick_edit_javascript');
347+
function ashuwp_quick_edit_javascript() {
348+
$current_screen = get_current_screen();
349+
if (!is_object($current_screen) || ($current_screen->post_type != 'sites'))return;
350+
if($current_screen->id == 'edit-sites'){
351+
echo"
352+
<script type='text/javascript'>
353+
jQuery(function($){
354+
var wp_inline_edit_function = inlineEditPost.edit;
355+
inlineEditPost.edit = function( post_id ) {
356+
wp_inline_edit_function.apply( this, arguments );
357+
var id = 0;
358+
if ( typeof( post_id ) == 'object' ) {
359+
id = parseInt( this.getId( post_id ) );
360+
}
361+
if ( id > 0 ) {
362+
var specific_post_edit_row = $( '#edit-' + id ),
363+
specific_post_row = $( '#post-' + id ),
364+
product_price = $( '.column-ordinal', specific_post_row ).text();
365+
366+
$('input[name=\"ordinal\"]', specific_post_edit_row ).val( product_price );
367+
}
368+
}
369+
});
370+
</script>";
371+
}
372+
if($current_screen->id == 'edit-favorites'){
373+
echo"
374+
<script type='text/javascript'>
375+
jQuery(function($){
376+
var wp_inline_edit_function = inlineEditTax.edit;
377+
inlineEditTax.edit = function( post_id ) {
378+
wp_inline_edit_function.apply( this, arguments );
379+
var id = 0;
380+
if ( typeof( post_id ) == 'object' ) {
381+
id = parseInt( this.getId( post_id ) );
382+
}
383+
console.log('调试区'+id);
384+
if ( id > 0 ) {
385+
var specific_post_edit_row = $( '#edit-' + id ),
386+
specific_post_row = $( '#tag-' + id ),
387+
product_price = $( '.column-ca_ordinal', specific_post_row ).text();
388+
389+
$('input[name=\"ca_ordinal\"]', specific_post_edit_row ).val( product_price );
390+
}
391+
}
392+
});
393+
</script>";
394+
}
395+
}
396+

style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Theme Name:WebStack
33
Theme URI:https://www.iowen.cn
44
Description:by 一为!官方网站:<a href="https://www.iowen.cn">一为忆</a>
5-
Version:1.1208.1
5+
Version:1.1210
66
Author:iowen
77
Author URI: https://www.iowen.cn/
88
*/

0 commit comments

Comments
 (0)