Intuitive Custom Post Order - Version 2.1.0

Version Description

  • Fixed bug: Custom Query which uses 'order' or 'orderby' parameters is preferred.
  • It does not depend on the designation manner of arguments( Parameters ). ( $args
Download this release

Release Info

Developer hijiri
Plugin Icon 128x128 Intuitive Custom Post Order
Version 2.1.0
Comparing to
See all releases

Code changes from version 2.0.8 to 2.1.0

Files changed (4) hide show
  1. admin/settings.php +42 -19
  2. intuitive-custom-post-order.php +185 -133
  3. js/hicpo.js +13 -12
  4. readme.txt +25 -9
admin/settings.php CHANGED
@@ -23,29 +23,52 @@ $objects = $hicpo_options['objects'];
23
 
24
  <?php if ( function_exists( 'wp_nonce_field' ) ) wp_nonce_field( 'nonce_hicpo' ); ?>
25
 
 
 
26
  <table class="form-table">
27
- <tbody>
28
- <tr valign="top">
29
- <th scope="row"><label for="blogname"><?php _e('Sortable Objects', 'hicpo') ?></label></th>
30
- <td>
31
- <?php
32
- $post_types = get_post_types( array (
33
- 'public' => true
34
- ), 'objects' );
35
-
36
- foreach ($post_types as $post_type ) {
37
- if ( $post_type->name != 'attachment' ) {
38
- ?>
39
- <label><input type="checkbox" name="objects[]" value="<?php echo $post_type->name; ?>" <?php if ( isset($objects) && is_array($objects) ) { if ( in_array($post_type->name, $objects )) { echo 'checked="checked"'; } } ?> />&nbsp;<?php echo $post_type->label; ?></label><br />
40
- <?php
 
41
  }
42
- }
43
- ?>
44
- </td>
45
- </tr>
46
- </tbody>
47
  </table>
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  <p class="submit">
50
  <input type="submit" class="button-primary" name="hicpo_submit" value="<?php _e('Update', 'cptg'); ?>" />
51
  </p>
23
 
24
  <?php if ( function_exists( 'wp_nonce_field' ) ) wp_nonce_field( 'nonce_hicpo' ); ?>
25
 
26
+ <div id="hicpo_select_objects">
27
+
28
  <table class="form-table">
29
+ <tbody>
30
+ <tr valign="top">
31
+ <th scope="row"><?php _e('Sortable Objects', 'hicpo') ?></th>
32
+ <td>
33
+ <?php
34
+ $post_types = get_post_types( array (
35
+ 'public' => true
36
+ ), 'objects' );
37
+
38
+ foreach ($post_types as $post_type ) {
39
+ if ( $post_type->name != 'attachment' ) {
40
+ ?>
41
+ <label><input type="checkbox" name="objects[]" value="<?php echo $post_type->name; ?>" <?php if ( isset($objects) && is_array($objects) ) { if ( in_array($post_type->name, $objects )) { echo 'checked="checked"'; } } ?> />&nbsp;<?php echo $post_type->label; ?></label><br />
42
+ <?php
43
+ }
44
  }
45
+ ?>
46
+ </td>
47
+ </tr>
48
+ </tbody>
 
49
  </table>
50
 
51
+ </div>
52
+
53
+ <label><input type="checkbox" id="hicpo_allcheck"> <?php _e('All Check', 'hicpo') ?></label>
54
+
55
+ <script>
56
+ (function($){
57
+
58
+ $("#hicpo_allcheck").on('click', function(){
59
+
60
+ var items = $("#hicpo_select_objects input");
61
+
62
+ if ( $(this).is(':checked') ) {
63
+ $(items).prop('checked', true);
64
+ } else {
65
+ $(items).prop('checked', false);
66
+ }
67
+ });
68
+
69
+ })(jQuery)
70
+ </script>
71
+
72
  <p class="submit">
73
  <input type="submit" class="button-primary" name="hicpo_submit" value="<?php _e('Update', 'cptg'); ?>" />
74
  </p>
intuitive-custom-post-order.php CHANGED
@@ -3,12 +3,12 @@
3
  Plugin Name: Intuitive Custom Post Order
4
  Plugin URI: http://hijiriworld.com/web/plugins/intuitive-custom-post-order/
5
  Description: Intuitively, Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
6
- Version: 2.0.8
7
  Author: hijiri
8
  Author URI: http://hijiriworld.com/web/
9
  */
10
 
11
- /* Copyright 2013 hijiri
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License, version 2, as
@@ -24,11 +24,11 @@ Author URI: http://hijiriworld.com/web/
24
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
 
27
- /***************************************************************
28
 
29
  Define
30
 
31
- ***************************************************************/
32
 
33
  define( 'HICPO_URL', plugins_url('', __FILE__) );
34
 
@@ -36,11 +36,11 @@ define( 'HICPO_DIR', plugin_dir_path(__FILE__) );
36
 
37
  load_plugin_textdomain( 'hicpo', false, basename(dirname(__FILE__)).'/lang' );
38
 
39
- /***************************************************************
40
 
41
  Class & Method
42
 
43
- ***************************************************************/
44
 
45
  $hicpo = new Hicpo();
46
 
@@ -48,13 +48,14 @@ class Hicpo
48
  {
49
  function __construct()
50
  {
 
51
  if ( !get_option('hicpo_options') ) $this->hicpo_install();
52
 
53
  add_action( 'admin_menu', array( &$this, 'admin_menu') );
54
 
55
  add_action( 'admin_init', array( &$this, 'refresh' ) );
56
  add_action( 'admin_init', array( &$this, 'update_options') );
57
- add_action( 'init', array( &$this, 'load_script_css' ) );
58
 
59
  add_action( 'wp_ajax_update-menu-order', array( &$this, 'update_menu_order' ) );
60
 
@@ -62,11 +63,14 @@ class Hicpo
62
  add_filter( 'pre_get_posts', array( &$this, 'hicpo_filter_active' ) );
63
  add_filter( 'pre_get_posts', array( &$this, 'hicpo_pre_get_posts' ) );
64
 
65
- // previous_post_link(), next_post_link()
66
  add_filter( 'get_previous_post_where', array( &$this, 'hicpo_previous_post_where' ) );
67
  add_filter( 'get_previous_post_sort', array( &$this, 'hicpo_previous_post_sort' ) );
68
  add_filter( 'get_next_post_where', array( &$this, 'hocpo_next_post_where' ) );
69
  add_filter( 'get_next_post_sort', array( &$this, 'hicpo_next_post_sort' ) );
 
 
 
70
  }
71
 
72
  function hicpo_install()
@@ -79,35 +83,28 @@ class Hicpo
79
  'public' => true
80
  ), 'objects' );
81
 
 
 
82
  foreach ($post_types as $post_type ) {
83
- $init_objects[] = $post_type->name;
 
 
84
  }
85
  $input_options = array( 'objects' => $init_objects );
86
 
87
  update_option( 'hicpo_options', $input_options );
88
 
89
-
90
- // Initialize : menu_order from date_post
91
 
92
  $hicpo_options = get_option( 'hicpo_options' );
93
  $objects = $hicpo_options['objects'];
94
 
95
- foreach( $objects as $object) {
96
- $sql = "SELECT
97
- ID
98
- FROM
99
- $wpdb->posts
100
- WHERE
101
- post_type = '".$object."'
102
- AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
103
- ORDER BY
104
- post_date DESC
105
- ";
106
-
107
- $results = $wpdb->get_results($sql);
108
-
109
- foreach( $results as $key => $result ) {
110
- $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
111
  }
112
  }
113
  }
@@ -121,8 +118,8 @@ class Hicpo
121
  {
122
  require HICPO_DIR.'admin/settings.php';
123
  }
124
-
125
- function _check_active_objects() {
126
 
127
  $active = false;
128
 
@@ -130,63 +127,66 @@ class Hicpo
130
  $objects = $hicpo_options['objects'];
131
 
132
  if ( is_array( $objects ) ) {
133
- strstr( $_SERVER["REQUEST_URI"], 'wp-admin/post-new.php' );
134
- // for Pages or Custom Post Types
135
- if ( isset($_GET['post_type']) ) {
136
- // not post new page
137
- if ( in_array( $_GET['post_type'], $objects ) && !strstr( $_SERVER["REQUEST_URI"], 'wp-admin/post-new.php' ) ) {
138
- $active = true;
139
- }
140
- // for Posts
141
- } else {
142
- $post_list = strstr( $_SERVER["REQUEST_URI"], 'wp-admin/edit.php' );
143
- if ( $post_list && in_array( 'post', $objects ) ) {
144
- $active = true;
 
145
  }
146
  }
147
  }
148
- if ( $active ) return true;
149
  }
150
-
151
  function load_script_css() {
152
- if ( $this->_check_active_objects() ) {
 
 
153
  // load JavaScript
154
- wp_enqueue_script( 'jQuery' );
 
155
  wp_enqueue_script( 'jquery-ui-sortable' );
156
  wp_enqueue_script( 'hicpojs', HICPO_URL.'/js/hicpo.js', array( 'jquery' ), null, true );
 
157
  // load CSS
 
158
  wp_enqueue_style( 'hicpo', HICPO_URL.'/css/hicpo.css', array(), null );
159
  }
160
  }
161
 
162
  function refresh()
163
  {
164
- if ( $this->_check_active_objects() ) {
165
- // menu_orderを再構築する
166
- global $wpdb;
167
-
168
- $hicpo_options = get_option( 'hicpo_options' );
169
- $objects = $hicpo_options['objects'];
170
-
171
- if ( is_array( $objects ) ) {
172
- foreach( $objects as $object) {
173
- $sql = "SELECT
174
- ID
175
- FROM
176
- $wpdb->posts
177
- WHERE
178
- post_type = '".$object."'
179
- AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
180
- ORDER BY
181
- menu_order ASC
182
- ";
183
-
184
- $results = $wpdb->get_results($sql);
185
-
186
- foreach( $results as $key => $result ) {
187
- // 新規追加した場合「menu_order=0」で登録されるため、常に1からはじまるように振っておく
188
- $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
189
- }
190
  }
191
  }
192
  }
@@ -202,21 +202,22 @@ class Hicpo
202
 
203
  // ページに含まれる記事のIDをすべて取得
204
  $id_arr = array();
 
205
  foreach( $data as $key => $values ) {
206
  foreach( $values as $position => $id ) {
207
  $id_arr[] = $id;
208
  }
209
  }
210
 
211
- // ページに含まれる記事のmenu_orderをすべて取得
212
  $menu_order_arr = array();
213
  foreach( $id_arr as $key => $id ) {
214
- $results = $wpdb->get_results("SELECT menu_order FROM $wpdb->posts WHERE ID = ".$id);
215
  foreach( $results as $result ) {
216
  $menu_order_arr[] = $result->menu_order;
217
  }
218
  }
219
- // menu_order配列をソート(キーと値の相関関係は維持しない)
220
  sort($menu_order_arr);
221
 
222
  foreach( $data as $key => $values ) {
@@ -229,6 +230,8 @@ class Hicpo
229
 
230
  function update_options()
231
  {
 
 
232
  if ( isset( $_POST['hicpo_submit'] ) ) {
233
 
234
  check_admin_referer( 'nonce_hicpo' );
@@ -240,6 +243,27 @@ class Hicpo
240
  }
241
 
242
  update_option( 'hicpo_options', $input_options );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  wp_redirect( 'admin.php?page=hicpo-settings&msg=update' );
244
  }
245
  }
@@ -254,7 +278,7 @@ class Hicpo
254
  if ( in_array( $post->post_type, $objects ) ) {
255
  $current_menu_order = $post->menu_order;
256
  $where = "WHERE p.menu_order > '".$current_menu_order."' AND p.post_type = '". $post->post_type ."' AND p.post_status = 'publish'";
257
- }
258
  return $where;
259
  }
260
 
@@ -308,17 +332,22 @@ class Hicpo
308
 
309
  function hicpo_pre_get_posts( $wp_query )
310
  {
 
 
311
  $hicpo_options = get_option('hicpo_options');
312
  $objects = $hicpo_options['objects'];
313
 
314
  if ( is_array( $objects ) ) {
315
 
316
- // for Admin ---------------------------------------------------------------
317
 
318
  if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
319
-
320
- // post_type=post or page or custom post type
321
- // adminの場合、post_tyope=postも渡される
 
 
 
322
  if ( isset( $wp_query->query['post_type'] ) ) {
323
  if ( in_array( $wp_query->query['post_type'], $objects ) ) {
324
  $wp_query->set( 'orderby', 'menu_order' );
@@ -326,80 +355,103 @@ class Hicpo
326
  }
327
  }
328
 
329
- // for Template ------------------------------------------------------------
330
 
331
  } else {
332
 
333
- $active = false;
 
 
334
 
335
- // postsのWordpressループ ----------------
 
336
 
337
- // $wp_query->queryが空配列の場合
338
- // WordPressループでもposts以外はpost_typeが渡される
 
 
 
339
 
340
- if ( empty( $wp_query->query ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
  if ( in_array( 'post', $objects ) ) {
342
  $active = true;
343
  }
344
- } else {
 
 
345
 
346
- // get_posts() ----------------------
347
 
348
- // 完全な判別ではないが、suppress_filtersパラメータの有無で判別
349
- // get_posts()の場合、post_type, orderby, orderパラメータは必ず渡される
350
 
351
- if ( isset($wp_query->query['suppress_filters']) ) {
352
-
353
- // post_type判定
354
- if ( is_array( $wp_query->query['post_type'] ) ) {
355
- $post_types = $wp_query->query['post_type'];
356
- foreach( $post_types as $post_type ) {
357
- if ( in_array( $post_type, $objects ) ) {
358
- $active = true;
359
- }
360
- }
361
- } else {
362
- if ( in_array( $wp_query->query['post_type'], $objects ) ) {
363
- $active = true;
364
  }
365
- }
366
-
367
- // query_posts() or WP_Query()
368
- } else {
369
-
370
- // post_typeが指定されている場合
371
- if ( isset( $wp_query->query['post_type'] ) ) {
372
-
373
- // post_type判定
374
- if ( is_array( $wp_query->query['post_type'] ) ) {
375
- $post_types = $wp_query->query['post_type'];
376
- foreach( $post_types as $post_type ) {
377
- if ( in_array( $post_type, $objects ) ) {
378
- $active = true;
379
- }
380
- }
381
- } else {
382
- if ( in_array( $wp_query->query['post_type'], $objects ) ) {
383
- $active = true;
384
- }
385
  }
386
- // post_typeが指定されてい場合はpost_type=post
387
  } else {
388
- if ( in_array( 'post', $objects ) ) {
389
- $active = true;
 
 
 
 
390
  }
391
  }
392
- }
 
 
 
393
  }
394
-
395
- if ( $active ) {
396
- if ( !isset( $wp_query->query['orderby'] ) || $wp_query->query['orderby'] == 'date' ) $wp_query->set( 'orderby', 'menu_order' );
397
- if ( !isset( $wp_query->query['order'] ) || $wp_query->query['order'] == 'DESC' ) $wp_query->set( 'order', 'ASC' );
398
- }
399
  }
400
  }
401
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  }
403
-
404
-
405
  ?>
3
  Plugin Name: Intuitive Custom Post Order
4
  Plugin URI: http://hijiriworld.com/web/plugins/intuitive-custom-post-order/
5
  Description: Intuitively, Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
6
+ Version: 2.1.0
7
  Author: hijiri
8
  Author URI: http://hijiriworld.com/web/
9
  */
10
 
11
+ /* Copyright 2014 hijiri
12
 
13
  This program is free software; you can redistribute it and/or modify
14
  it under the terms of the GNU General Public License, version 2, as
24
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
  */
26
 
27
+ /***********************************************************************************
28
 
29
  Define
30
 
31
+ ***********************************************************************************/
32
 
33
  define( 'HICPO_URL', plugins_url('', __FILE__) );
34
 
36
 
37
  load_plugin_textdomain( 'hicpo', false, basename(dirname(__FILE__)).'/lang' );
38
 
39
+ /***********************************************************************************
40
 
41
  Class & Method
42
 
43
+ ***********************************************************************************/
44
 
45
  $hicpo = new Hicpo();
46
 
48
  {
49
  function __construct()
50
  {
51
+
52
  if ( !get_option('hicpo_options') ) $this->hicpo_install();
53
 
54
  add_action( 'admin_menu', array( &$this, 'admin_menu') );
55
 
56
  add_action( 'admin_init', array( &$this, 'refresh' ) );
57
  add_action( 'admin_init', array( &$this, 'update_options') );
58
+ add_action( 'admin_init', array( &$this, 'load_script_css' ) );
59
 
60
  add_action( 'wp_ajax_update-menu-order', array( &$this, 'update_menu_order' ) );
61
 
63
  add_filter( 'pre_get_posts', array( &$this, 'hicpo_filter_active' ) );
64
  add_filter( 'pre_get_posts', array( &$this, 'hicpo_pre_get_posts' ) );
65
 
66
+ // previous_post(s)_link, next_post(s)_link
67
  add_filter( 'get_previous_post_where', array( &$this, 'hicpo_previous_post_where' ) );
68
  add_filter( 'get_previous_post_sort', array( &$this, 'hicpo_previous_post_sort' ) );
69
  add_filter( 'get_next_post_where', array( &$this, 'hocpo_next_post_where' ) );
70
  add_filter( 'get_next_post_sort', array( &$this, 'hicpo_next_post_sort' ) );
71
+
72
+ // for dev
73
+ //add_action( 'pre_get_posts', array( &$this, 'hicpo_dev' ) );
74
  }
75
 
76
  function hicpo_install()
83
  'public' => true
84
  ), 'objects' );
85
 
86
+ $init_objects = array();
87
+
88
  foreach ($post_types as $post_type ) {
89
+ if ( $post_type->name != 'attachment' ) {
90
+ $init_objects[] = $post_type->name;
91
+ }
92
  }
93
  $input_options = array( 'objects' => $init_objects );
94
 
95
  update_option( 'hicpo_options', $input_options );
96
 
97
+ // Initialize : menu_order
 
98
 
99
  $hicpo_options = get_option( 'hicpo_options' );
100
  $objects = $hicpo_options['objects'];
101
 
102
+ if ( !empty( $objects ) ) {
103
+ foreach( $objects as $object) {
104
+ $results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') ORDER BY post_date DESC" );
105
+ foreach( $results as $key => $result ) {
106
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
107
+ }
 
 
 
 
 
 
 
 
 
 
108
  }
109
  }
110
  }
118
  {
119
  require HICPO_DIR.'admin/settings.php';
120
  }
121
+
122
+ function _check_load_script_css() {
123
 
124
  $active = false;
125
 
127
  $objects = $hicpo_options['objects'];
128
 
129
  if ( is_array( $objects ) ) {
130
+ // exlude addnew Page and edit Page
131
+ if ( !strstr( $_SERVER["REQUEST_URI"], 'action=edit' ) && !strstr( $_SERVER["REQUEST_URI"], 'wp-admin/post-new.php' ) ) {
132
+
133
+ // if Custom Post Types( include 'Page')
134
+ if ( isset( $_GET['post_type'] ) ) {
135
+ if ( in_array( $_GET['post_type'], $objects ) ) {
136
+ $active = true;
137
+ }
138
+ // if Post
139
+ } else if ( strstr( $_SERVER["REQUEST_URI"], 'wp-admin/edit.php' ) ) {
140
+ if ( in_array( 'post', $objects ) ) {
141
+ $active = true;
142
+ }
143
  }
144
  }
145
  }
146
+ return $active;
147
  }
148
+
149
  function load_script_css() {
150
+
151
+ if ( $this->_check_load_script_css() ) {
152
+
153
  // load JavaScript
154
+
155
+ wp_enqueue_script( 'jquery' );
156
  wp_enqueue_script( 'jquery-ui-sortable' );
157
  wp_enqueue_script( 'hicpojs', HICPO_URL.'/js/hicpo.js', array( 'jquery' ), null, true );
158
+
159
  // load CSS
160
+
161
  wp_enqueue_style( 'hicpo', HICPO_URL.'/css/hicpo.css', array(), null );
162
  }
163
  }
164
 
165
  function refresh()
166
  {
167
+ // menu_orderを再構築する
168
+ global $wpdb;
169
+
170
+ $hicpo_options = get_option( 'hicpo_options' );
171
+ $objects = $hicpo_options['objects'];
172
+
173
+ if ( is_array( $objects ) ) {
174
+ foreach( $objects as $object) {
175
+
176
+ /*
177
+ menu_order の max とレコード数が一致して、かつ min が 0 じゃない時は再構築処理をスキップする
178
+ */
179
+
180
+ $result = $wpdb->get_results( "SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min FROM $wpdb->posts WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')" );
181
+ if ( count( $result ) > 0 && $result[0]->cnt == $result[0]->max && $result[0]->min != 0 ) {
182
+ continue;
183
+ }
184
+
185
+ $results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') ORDER BY menu_order ASC" );
186
+
187
+ foreach( $results as $key => $result ) {
188
+ // 新規追加した場合 menu_order=0 で登録されるため、常に1からはじまるように振っておく
189
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
 
 
 
190
  }
191
  }
192
  }
202
 
203
  // ページに含まれる記事のIDをすべて取得
204
  $id_arr = array();
205
+
206
  foreach( $data as $key => $values ) {
207
  foreach( $values as $position => $id ) {
208
  $id_arr[] = $id;
209
  }
210
  }
211
 
212
+ // ページに含まれる記事の menu_order をすべて取得
213
  $menu_order_arr = array();
214
  foreach( $id_arr as $key => $id ) {
215
+ $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = ".$id );
216
  foreach( $results as $result ) {
217
  $menu_order_arr[] = $result->menu_order;
218
  }
219
  }
220
+ // menu_order 配列をソート(キーと値の相関関係は維持しない)
221
  sort($menu_order_arr);
222
 
223
  foreach( $data as $key => $values ) {
230
 
231
  function update_options()
232
  {
233
+ global $wpdb;
234
+
235
  if ( isset( $_POST['hicpo_submit'] ) ) {
236
 
237
  check_admin_referer( 'nonce_hicpo' );
243
  }
244
 
245
  update_option( 'hicpo_options', $input_options );
246
+
247
+ /*
248
+ はじめて有効化されたオブジェクトの場合、ディフォルト状態 order=post_date に従って menu_order を振っておく
249
+ */
250
+
251
+ $hicpo_options = get_option( 'hicpo_options' );
252
+ $objects = $hicpo_options['objects'];
253
+
254
+ if ( !empty( $objects ) ) {
255
+ foreach( $objects as $object) {
256
+ // 記事が1つ以上存在し、menu_oredr の最大値が 0 のオブジェクトが該当
257
+ $result = $wpdb->get_results( "SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min FROM $wpdb->posts WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')" );
258
+ if ( count( $result ) > 0 && $result[0]->max == 0 ) {
259
+ $results = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = '".$object."' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') ORDER BY post_date DESC" );
260
+ foreach( $results as $key => $result ) {
261
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
262
+ }
263
+ }
264
+ }
265
+ }
266
+
267
  wp_redirect( 'admin.php?page=hicpo-settings&msg=update' );
268
  }
269
  }
278
  if ( in_array( $post->post_type, $objects ) ) {
279
  $current_menu_order = $post->menu_order;
280
  $where = "WHERE p.menu_order > '".$current_menu_order."' AND p.post_type = '". $post->post_type ."' AND p.post_status = 'publish'";
281
+ }
282
  return $where;
283
  }
284
 
332
 
333
  function hicpo_pre_get_posts( $wp_query )
334
  {
335
+ global $args;
336
+
337
  $hicpo_options = get_option('hicpo_options');
338
  $objects = $hicpo_options['objects'];
339
 
340
  if ( is_array( $objects ) ) {
341
 
342
+ // for Admin >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
343
 
344
  if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
345
+
346
+ /*
347
+ post_type=post or page or custom post type
348
+ adminの場合、post_type=postも渡される
349
+ */
350
+
351
  if ( isset( $wp_query->query['post_type'] ) ) {
352
  if ( in_array( $wp_query->query['post_type'], $objects ) ) {
353
  $wp_query->set( 'orderby', 'menu_order' );
355
  }
356
  }
357
 
358
+ // for Template >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
359
 
360
  } else {
361
 
362
+ /*
363
+
364
+ post_type が有効オブジェクトかどうかを判別
365
 
366
+ $wp_query->query['post_type'] で判別
367
+ ディフォルトのクエリの場合、$args指定がないため
368
 
369
+ */
370
+
371
+ $active = false;
372
+
373
+ // post_typeが指定されている場合
374
 
375
+ if ( isset( $wp_query->query['post_type'] ) ) {
376
+ // 複数指定の場合は、いずれかひとつでも該当すれば有効
377
+ if ( is_array( $wp_query->query['post_type'] ) ) {
378
+ $post_types = $wp_query->query['post_type'];
379
+ foreach( $post_types as $post_type ) {
380
+ if ( in_array( $post_type, $objects ) ) {
381
+ $active = true;
382
+ }
383
+ }
384
+ } else {
385
+ if ( in_array( $wp_query->query['post_type'], $objects ) ) {
386
+ $active = true;
387
+ }
388
+ }
389
+ // post_typeが指定されていなければpost
390
+ } else {
391
  if ( in_array( 'post', $objects ) ) {
392
  $active = true;
393
  }
394
+ }
395
+
396
+ /*
397
 
398
+ $args が存在した場合はそちらを優先する
399
 
400
+ */
 
401
 
402
+ if ( $active ) {
403
+
404
+ if ( isset( $args ) ) {
405
+ // args = array( 'orderby' => 'date', 'order' => 'DESC' );
406
+ if ( is_array( $args ) ) {
407
+ if ( !isset( $args['orderby'] ) ) {
408
+ $wp_query->set( 'orderby', 'menu_order' );
 
 
 
 
 
 
409
  }
410
+ if ( !isset( $args['order'] ) ) {
411
+ $wp_query->set( 'order', 'ASC' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
  }
413
+ // args = 'orderby=date&order=DESC';
414
  } else {
415
+ if ( !strstr( $args, 'orderby=' ) ) {
416
+ $wp_query->set( 'orderby', 'menu_order' );
417
+ }
418
+ if ( !strstr( $args, 'order=' ) ) {
419
+ $wp_query->set( 'order', 'ASC' );
420
+
421
  }
422
  }
423
+ } else {
424
+ $wp_query->set( 'orderby', 'menu_order' );
425
+ $wp_query->set( 'order', 'ASC' );
426
+ }
427
  }
 
 
 
 
 
428
  }
429
  }
430
  }
431
+
432
+ // for dev
433
+ /*
434
+ function hicpo_dev( $query ) {
435
+
436
+ global $args;
437
+
438
+ echo '[$args]';
439
+ echo isset( $args ) ? ' isset ' : ' non-object ';
440
+ echo '/';
441
+ echo is_array( $args ) ? ' array ' : ' no-array ';
442
+ echo '<br>';
443
+ print_r($args);
444
+ echo '<br>';
445
+
446
+ echo '[$query->query]';
447
+ echo isset( $query->query ) ? ' isset ' : ' non-object ';
448
+ echo '/';
449
+ echo is_array( $query->query ) ? ' array ' : ' no-array ';
450
+ echo '<br>';
451
+ print_r($query->query);
452
+ echo '<br>';
453
+
454
+ }
455
+ */
456
  }
 
 
457
  ?>
js/hicpo.js CHANGED
@@ -1,21 +1,22 @@
1
- jQuery(document).ready(function() {
2
- jQuery("#the-list").sortable({
3
  'items': 'tr',
4
  'axis': 'y',
5
  'helper': fixHelper,
6
  'update' : function(e, ui) {
7
- jQuery.post( ajaxurl, {
8
  action: 'update-menu-order',
9
- order: jQuery("#the-list").sortable("serialize"),
10
  });
11
  }
12
  });
13
- //jQuery("#the-list").disableSelection();
14
- });
15
 
16
- var fixHelper = function(e, ui) {
17
- ui.children().children().each(function() {
18
- jQuery(this).width(jQuery(this).width());
19
- });
20
- return ui;
21
- };
 
 
1
+ (function($){
2
+ $("#the-list").sortable({
3
  'items': 'tr',
4
  'axis': 'y',
5
  'helper': fixHelper,
6
  'update' : function(e, ui) {
7
+ $.post( ajaxurl, {
8
  action: 'update-menu-order',
9
+ order: $("#the-list").sortable("serialize"),
10
  });
11
  }
12
  });
13
+ //$("#the-list").disableSelection();
 
14
 
15
+ var fixHelper = function(e, ui) {
16
+ ui.children().children().each(function() {
17
+ $(this).width($(this).width());
18
+ });
19
+ return ui;
20
+ };
21
+
22
+ })(jQuery)
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Intuitive Custom Post Order ===
2
  Contributors: hijiri
3
  Tags: post order, posts order, order post, order posts, custom post type order
4
- Requires at least: 3.8.0
5
- Tested up to: 3.8.0
6
- Stable tag: 2.0.8
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -15,7 +15,7 @@ Intuitively, Order Items (Posts, Pages, and Custom Post Types) using a Drag and
15
  Configuration is unnecessary.
16
  You can do directly on default WordPress administration.
17
 
18
- Excluding Custom Query which uses 'order' or 'orderby' parameters, in query_posts()', 'WP_Query()', and 'get_posts'.
19
 
20
  == Installation ==
21
 
@@ -29,6 +29,23 @@ Excluding Custom Query which uses 'order' or 'orderby' parameters, in query_post
29
 
30
  == Changelog ==
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  = 2.0.8 =
33
 
34
  * Performance improvement for Admin.
@@ -36,8 +53,7 @@ Excluding Custom Query which uses 'order' or 'orderby' parameters, in query_post
36
 
37
  = 2.0.7 =
38
 
39
- * Bug fixed.
40
- for WordPress 3.8
41
  * Add Swedish Translations.(by Thomas)
42
 
43
  = 2.0.6 =
@@ -50,7 +66,7 @@ Excluding Custom Query which uses 'order' or 'orderby' parameters, in query_post
50
 
51
  = 2.0.4 =
52
 
53
- * Bug fixed
54
 
55
  = 2.0.3 =
56
 
@@ -58,7 +74,7 @@ Excluding Custom Query which uses 'order' or 'orderby' parameters, in query_post
58
 
59
  = 2.0.2 =
60
 
61
- * Bug fixed
62
 
63
  = 2.0.0 =
64
 
@@ -82,7 +98,7 @@ Excluding Custom Query which uses 'order' or 'orderby' parameters, in query_post
82
 
83
  = 1.1.1 =
84
 
85
- * Bug fixed
86
 
87
  = 1.1.0 =
88
 
1
  === Intuitive Custom Post Order ===
2
  Contributors: hijiri
3
  Tags: post order, posts order, order post, order posts, custom post type order
4
+ Requires at least: 3.5.0
5
+ Tested up to: 3.8.1
6
+ Stable tag: 2.1.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
15
  Configuration is unnecessary.
16
  You can do directly on default WordPress administration.
17
 
18
+ Custom Query which uses 'order' or 'orderby' parameters is preferred. ( 'WP_Query()', 'get_posts()', 'query_posts()' )
19
 
20
  == Installation ==
21
 
29
 
30
  == Changelog ==
31
 
32
+ = 2.1.0 =
33
+
34
+ * Fixed bug: Custom Query which uses 'order' or 'orderby' parameters is preferred.
35
+ * It does not depend on the designation manner of arguments( Parameters ).
36
+ ( $args = 'orderby=&order=' or $args = array( 'orderby' => '', 'order' => '' ) )
37
+ * The trouble which exists in 2.0.7, 2.0.8, 2.0.9 was improved!
38
+ * From 2.0.6 please update in 2.1.0.
39
+
40
+ = 2.0.9 =
41
+
42
+ * Performance improvement for Admin.
43
+ Fatal performance problem was improved dramatically.
44
+ * Fixed bug: Attachment objects are not broken.
45
+ * Fixed bug: Alert warning on the multisite was solved.
46
+ * Fixed bug: First when enabling items, 'menu order' of items are not broken.
47
+ * Custom Query which uses 'order' or 'orderby' parameters is preferred.
48
+
49
  = 2.0.8 =
50
 
51
  * Performance improvement for Admin.
53
 
54
  = 2.0.7 =
55
 
56
+ * Fixed bug: for WordPress 3.8
 
57
  * Add Swedish Translations.(by Thomas)
58
 
59
  = 2.0.6 =
66
 
67
  = 2.0.4 =
68
 
69
+ * Fixed bug
70
 
71
  = 2.0.3 =
72
 
74
 
75
  = 2.0.2 =
76
 
77
+ * Fixed bug
78
 
79
  = 2.0.0 =
80
 
98
 
99
  = 1.1.1 =
100
 
101
+ * Fixed bug
102
 
103
  = 1.1.0 =
104