teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

va-simple-expiresを"利用している"という表現を、参考にしていると変更。コードを追記。

2016/07/02 02:38

投稿

rela
rela

スコア8

title CHANGED
@@ -1,1 +1,1 @@
1
- [Wordpress]csvで登録した投稿のカテゴリーを指定日時に変更する方法
1
+ [Wordpress]csvで登録した投稿のカテゴリーを指定日時に変更する方法
body CHANGED
@@ -14,17 +14,50 @@
14
14
  原因、もしくは目的の別達成方法などわかる方がいましたら、ご教授いただけると幸いです。
15
15
 
16
16
  ###試したこと
17
- [この記事](http://publictestzone.com/ptz/blog/set-expiration-date-to-wordpress-posts-imported-from-csv-file/)を参考に、該当するpostの取得には [va-simple-expires](https://ja.wordpress.org/plugins/va-simple-expires/)を利用させていただいています。
17
+ [この記事](http://publictestzone.com/ptz/blog/set-expiration-date-to-wordpress-posts-imported-from-csv-file/)を参考に、該当するpostの取得には [va-simple-expires](https://ja.wordpress.org/plugins/va-simple-expires/)を参考に組み立てています。[「利用させていただいています。」からご指摘により変更]
18
18
 
19
19
  `term_exists()`が動いているかはDebug Bar Consoleを利用して確認しました。
20
20
 
21
21
  `pub_end`には"2016/6/6"のような値を保存しています。
22
22
 
23
23
  ###該当のソースコード
24
+
25
+ **dr-simple-category-changer.php**
26
+
24
27
  ```php
25
- global $wpdb;
28
+ <?php
29
+ if ( !function_exists( 'dr_simple_category_changer_setup' ) ) :
30
+ function dr_simple_category_changer_setup() {
31
+ if ( !class_exists( 'DR_SIMPLE_CATEGORY_CHANGER_SETUP' ) ) {
32
+ class DR_SIMPLE_CATEGORY_CHANGER_SETUP extends _DR_SIMPLE_CATEGORY_CHANGER {
33
+ protected function __construct() {
34
+ parent::__construct();
35
+ }
36
+ }
37
+ }
26
38
 
39
+ $dr_simple_category_changer = DR_SIMPLE_CATEGORY_CHANGER_SETUP::instance();
40
+ }
41
+ endif; // dr_simple_category_changer_setup
42
+ add_action( 'plugins_loaded', 'dr_simple_category_changer_setup' );
43
+
44
+ ```
45
+
46
+ ** 以下は同一ファイル内`class _DR_SIMPLE_CATEGORY_CHANGER` 内になります**
47
+
48
+ ```php
49
+ protected function __construct() {
50
+ add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
51
+ add_action( 'init', array( &$this, 'simple_category_changer' ) );
52
+ }
53
+ ```
54
+
55
+ ```php
56
+
57
+ function simple_category_changer() {
58
+ global $wpdb;
59
+
27
- $result = $wpdb->get_results( $wpdb->prepare("
60
+ $result = $wpdb->get_results( $wpdb->prepare("
28
61
  SELECT postmetadate.post_id
29
62
  FROM $wpdb->postmeta AS postmetadate, $wpdb->postmeta AS postmetadoit, $wpdb->posts AS posts
30
63
  WHERE postmetadoit.meta_key = 'iscsv'
@@ -37,15 +70,17 @@
37
70
  ", current_time("mysql") ) );
38
71
 
39
72
 
40
- $term = term_exists('past', 'category');
73
+ $term = term_exists('past', 'category');
41
74
 
42
75
 
43
- if ( ! empty( $result ) && !empty($term )) :
76
+ if ( ! empty( $result ) && !empty($term )) :
44
- $update_post = array();
77
+ $update_post = array();
45
- foreach ( $result as $cur_post ) :
78
+ foreach ( $result as $cur_post ) :
46
79
  remove_action( 'simple_category_changer','save_post' );
47
80
  wp_set_post_status($cur_post->post_id, $term->$term_id, 'category', false);
48
81
  add_action( 'simple_category_changer','save_post' );
49
- endforeach;
82
+ endforeach;
50
- endif;
83
+ endif;
84
+
85
+ }
51
86
  ```