やったこと

webサービスを作るときに考えたことを垂れ流します

【Wordpress】Smart Custom Fieldsで他人の関連投稿を表示させない!

「Smart Custom Fields」というワードプレスプラグインで、「関連投稿」というカスタムフィールドを作ることができる。
この「関連投稿」で、他人が作った投稿は表示させないで、自分が作った投稿だけを扱えるようにしたかった。
でもどうしてもそういう設定がなかった・・。

しかたがないのでプラグイン内のファイルに直接手を入れることにした。

修正するファイルは「/plugins/smart-custom-fields/classes/fields/class.field-related-posts.php

まずは、80行目あたりを下記のように書き換えた。
ajaxで動的に記事を読み込む箇所を、自分の記事限定とするように修正。
(でも管理者の権限の人だけは全ての記事を見ることができる)

        /**
         * Process that loading post when clicking post load button
         */
        public function relational_posts_search() {
                check_ajax_referer( SCF_Config::NAME . '-relation-post-types', 'nonce' );
                $_posts = array();
                if ( isset( $_POST['post_types'] ) ) {
                        $post_type = explode( ',', $_POST['post_types'] );
                        $args      = array(
                                'post_type'      => $post_type,
                                'order'          => 'ASC',
                                'orderby'        => 'ID',
                                'posts_per_page' => -1,
                                'post_status'    => 'any',
                        );

//MySetting
if (is_admin() && !current_user_can('administrator')){
  global $current_user;
  $args = array(
    'post_type'      => $post_type,
    'order'          => 'ASC',
    'orderby'        => 'ID',
    'posts_per_page' => -1,
    'post_status'    => 'any',
    'author'         => $current_user->ID
  );
}
//MySetting


あともう一か所、150行目あたりの以下の部分を書き換えた。
これは最初にページロードされたときにコンテンツを表示する部分。

        public function get_field( $index, $value ) {
                $name      = $this->get_field_name_in_editor( $index );
                $disabled  = $this->get_disable_attribute( $index );
                $post_type = $this->get( 'post-type' );
                $limit     = $this->get( 'limit' );
                if ( ! $post_type ) {
                        $post_type = array( 'post' );
                }
                if ( ! preg_match( '/^\d+$/', $limit ) ) {
                        $limit = '';
                }
                $posts_per_page = get_option( 'posts_per_page' );

                $args = array(
                        'post_type'      => $post_type,
                        'order'          => 'ASC',
                        'orderby'        => 'ID',
                        'posts_per_page' => $posts_per_page,
                        'post_status'    => 'any',
                );

//MySetting
if (is_admin() && !current_user_can('administrator')){
  global $current_user;
  $args = array(
    'post_type'      => $post_type,
    'order'          => 'ASC',
    'orderby'        => 'ID',
    'posts_per_page' => $posts_per_page,
    'post_status'    => 'any',
    'author'         => $current_user->ID
  );
}
//MySetting

この二か所を修正すればおk。

プラグインにはできるだけ手を入れたくなかったんだけど、他にいい方法思いつかなかったからしょうがないよね・・。