web-dev-qa-db-ja.com

Wordpressのグリッドビューにカスタム投稿タイプの値を表示する

図のようにWordPressでカスタム投稿タイプを作成しました。

enter image description here 私が欲しいのは、管理パネルのグリッド表示でこのカスタム投稿に追加された値を表示することです。これで、管理パネルには投稿のタイトル、日付、コメントの画像しか表示されなくなりましたが、カスタム投稿に入力されたPair2、buy sell、activityフィールドの値を表示したいと思います。私はたくさん検索しましたが、解決策が見つかりませんでした。 WordPressでどうすればいいの? enter image description here

_ edit _
これが私のカスタム投稿の作り方です

   <?php
if( ! function_exists( 'quote_create_post_type' ) ) :
  function quote_create_post_type() {
    $labels = array(
      'name' => __( 'ForexPair' ),
      'singular_name' => __( 'ForexPair' ),
      'add_new' => __( 'Add Pair Entry' ),
      'all_items' => __( 'All Pairs' ),
      'add_new_item' => __( 'Add Pair' ),
      'edit_item' => __( 'Edit Pair' ),
      'new_item' => __( 'New Pair' ),
      'view_item' => __( 'View Pair' ),
      'search_items' => __( 'Search Pairs' ),
      'not_found' => __( 'No pair found' ),
      'not_found_in_trash' => __( 'No pair found in trash' ),
      'parent_item_colon' => __( 'Parent pair' )
      //'menu_name' => default to 'name'
    );
    $args = array(
      'labels' => $labels,
      'public' => true,
      'has_archive' => true,
      'publicly_queryable' => true,
      'query_var' => true,
      'rewrite' => true,
      'capability_type' => 'post',
      'hierarchical' => false,
      'supports' => array(
        'title'
      ),
      'register_meta_box_cb' => 'quote_add_post_type_metabox'
    );
    register_post_type( 'quote', $args );
    //flush_rewrite_rules();

  }
  add_action( 'init', 'quote_create_post_type' );


  function quote_add_post_type_metabox() { // add the meta box
    //add_meta_box( 'quote_metabox', 'quote_metabox', 'quote', 'normal' );
    add_meta_box( 'quote_metabox', 'Pair Entries', 'quote_metabox', 'quote', 'normal' );
  }


  function quote_metabox() {
    global $post;
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="quote_post_noncename" id="quote_post_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the data if its already been entered
    $quote_post_entryprice = get_post_meta($post->ID, '_quote_post_entryprice', true);
    $quote_post_stoploss = get_post_meta($post->ID, '_quote_post_stoploss', true);
    $quote_post_pairname = get_post_meta($post->ID, '_quote_post_pairname', true);
    $quote_post_buysell = get_post_meta($post->ID, '_quote_post_buysell', true);
    $quote_post_activity = get_post_meta($post->ID, '_quote_post_activity', true);
    $quote_post_takeprofit = get_post_meta($post->ID, '_quote_post_takeprofit', true);
    // Echo out the field
    ?>
   <link rel="stylesheet" href="http://gripforex.com/wp-content/themes/Forsy/select2.css" />
  <script type="text/javascript" src="http://gripforex.com/wp-content/themes/Forsy/jquery-1.7.2.min.js"></script>
  <script type="text/javascript" src="http://gripforex.com/wp-content/themes/Forsy/jquery-ui-1.10.3.custom.min.js"></script>
  <script type="text/javascript" src="http://gripforex.com/wp-content/themes/Forsy/select2.js"></script>
  <style>
  .lbl{
  width: 86px;
display: block;
line-height: 20px;
  }
  </style>
    <div class="width_full p_box">
      <p>

        <label class="lbl">Pair:</label>
  <script>
        $(document).ready(function() { $("#e1").select2({

      matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())==0; }
});
    });
    $(document).ready(function() { $("#e2").select2({
      matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())==0; }
}); });
    $(document).ready(function() { $("#e3").select2({
      matcher: function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())==0; }
}); });
    </script>

  <select name="quote_post_pairname" id="e1" style="width:144px;">
  <option value="AUD/CAD" <?php if($quote_post_pairname=="AUD/CAD") echo "selected"; ?> >AUD/CAD</option>
  <option value="AUD/CHF" <?php if($quote_post_pairname=="AUD/CHF") echo "selected"; ?> >AUD/CHF</option>



  </select></p>
        <p>
        <label class="lbl">Buy Sell
          <select name="quote_post_buysell" id="e2" style="width:144px;">
            <option value="Buy" <?php if($quote_post_buysell=="Buy") echo "selected"; ?> >Buy</option>
            <option value="Sell" <?php if($quote_post_buysell=="Sell") echo "selected"; ?> >Sell</option>
          </select>
        </label>
      </p>
        <p>
        <label class="lbl">Activity
          <select name="quote_post_activity" id="e3" style="width:144px;">
            <option value="Get Ready" <?php if($quote_post_activity=="Get Ready") echo "selected"; ?> >Get Ready</option>
            <option value="Active" <?php if($quote_post_activity=="Active") echo "selected"; ?> >Active</option>
          </select>
        </label>
      </p>      
      <p>
        <label class="lbl">Entry Price<br>
          <input type="text" name="quote_post_entryprice" class="widefat" value="<?php echo $quote_post_entryprice; ?>">
        </label>
      </p>
      <p><label class="lbl">Stop Loss<br>
          <input type="text" name="quote_post_stoploss" value="<?php echo $quote_post_stoploss; ?>" class="widefat" />
        </label>
      </p>
      <p><label class="lbl">Take Profit<br>
          <input type="text" name="quote_post_takeprofit" value="<?php echo $quote_post_takeprofit; ?>" class="widefat" />
        </label>
      </p>      
    </div>
  <?php
  }


  function quote_post_save_meta( $post_id, $post ) { // save the data

    if( !wp_verify_nonce( $_POST['quote_post_noncename'], plugin_basename(__FILE__) ) ) {
      return $post->ID;
    }

    // is the user allowed to edit the post or page?
    if( ! current_user_can( 'edit_post', $post->ID )){
      return $post->ID;
    }

    $quote_post_meta['_quote_post_entryprice'] = $_POST['quote_post_entryprice'];
    $quote_post_meta['_quote_post_stoploss'] = $_POST['quote_post_stoploss'];
    $quote_post_meta['_quote_post_pairname'] = $_POST['quote_post_pairname'];
    $quote_post_meta['_quote_post_buysell'] = $_POST['quote_post_buysell'];
    $quote_post_meta['_quote_post_activity'] = $_POST['quote_post_activity'];
    $quote_post_meta['_quote_post_takeprofit'] = $_POST['quote_post_takeprofit'];
    // add values as custom fields
    foreach( $quote_post_meta as $key => $value ) { // cycle through the $quote_post_meta array
      // if( $post->post_type == 'revision' ) return; // don't store custom data twice
      $value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
      if( get_post_meta( $post->ID, $key, FALSE ) ) { // if the custom field already has a value
        update_post_meta($post->ID, $key, $value);
      } else { // if the custom field doesn't have a value
        add_post_meta( $post->ID, $key, $value );
      }
      if( !$value ) { // delete if blank
        delete_post_meta( $post->ID, $key );
      }
    }
  }
  add_action( 'save_post', 'quote_post_save_meta', 1, 2 ); // save the custom fields
endif; // end of function_exists()


if( ! function_exists( 'view_quotes_posts' ) ) : // output
  function view_quotes_posts( $num = 4, $do_shortcode = 1, $strip_shortcodes = 0 ) {

    $args = array(
      'numberposts'     => $num,
      'offset'          => 0,
      //'category'        => ,
      'orderby'         => 'menu_order, post_date', // post_date, Rand
      'order'           => 'DESC',

      'post_type'       => 'quote',

      'post_status'     => 'publish',
      'suppress_filters' => true,
    );

    $posts = get_posts( $args );
    global $wpdb;

 $querystr = "select $wpdb->posts.* 
    FROM $wpdb->posts
  WHERE $wpdb->posts.post_type = 'quote'
  ";


    $html = '<table><tr><th>Pair</th><th>Action</th><th>Status</th><th>Entry Price</th><th>Stop Loss</th><th>Take Profit</th></tr>';
    foreach ( $posts as $post ) {
      $meta_entryprice = get_post_meta( $post->ID, '_quote_post_entryprice', true );
      $meta_stoploss = get_post_meta( $post->ID, '_quote_post_stoploss', true );
      $meta_pairname = get_post_meta( $post->ID, '_quote_post_pairname', true );
      $meta_buysell = get_post_meta( $post->ID, '_quote_post_buysell', true );
      $meta_activity = get_post_meta( $post->ID, '_quote_post_activity', true );
      $meta_takeprofit = get_post_meta( $post->ID, '_quote_post_takeprofit', true );


      $html.='<tr>';
      $html .= '

          <td>'.$meta_pairname.'</td>
          <td>'.$meta_buysell.'</td>
          <td>'.$meta_activity.'</td>
          <td>'.$meta_entryprice.'</td>

          <td>'.$meta_stoploss.'</td>

          <td>'.$meta_takeprofit.'</td>
        ';
      $html.='</tr>';
    }
    $html .="</table>";
    $html = '<div class="wrapper">'.$html.'</div>';
    return $html;
  }
endif; 
?>
2
james

manage_edit-${post_type}_columnsフィルタをmanage_${post_type}_posts_custom_columnアクションと組み合わせて使用​​できます。 Codex内の example を確認して、ステップごとに分解してください。

まず、登録したカスタム投稿タイプの正しい名前を必ず入力してください。投稿タイプをquoteという名前で登録しました。

次に、追加する列を定義し(set_custom_edit_quote_columnsを参照)、適切な値を割り当てます(custom_quote_columnsを参照)。

あなたはこのようなものになってしまうはずです:

add_filter( 'manage_edit-quote_columns', 'set_custom_edit_quote_columns' );
add_action( 'manage_quote_posts_custom_column' , 'custom_quote_columns', 10, 2 );

function set_custom_edit_quote_columns( $columns ) {

    $columns['pair'] = __( 'Pair' );
    $columns['buysell'] = __( 'Buy Sell' );
    $columns['activity'] = __( 'Activity' );

    $columns['entryprice'] = __( 'Entry Price' );
    $columns['stoploss'] = __( 'Stop Loss' );
    $columns['takeprofit'] = __( 'Take Profit' );

    return $columns;
}

function custom_quote_columns( $column, $post_id ) {
    switch ( $column ) {

        case 'pair' :
            echo get_post_meta( $post_id, '_quote_post_pairname', true );
            break;

        case 'buysell' :
            echo get_post_meta( $post_id, '_quote_post_buysell', true );
            break;

        case 'activity' :
            echo get_post_meta( $post_id, '_quote_post_activity', true );
            break;

        case 'entryprice' :
            echo get_post_meta( $post_id, '_quote_post_entryprice', true );
            break;

        case 'stoploss' :
            echo get_post_meta( $post_id, '_quote_post_stoploss', true );
            break;

        case 'takeprofit' :
            echo get_post_meta( $post_id, '_quote_post_takeprofit', true );
            break;

    }
}
1
Sven