MW Formで「 問い合わせデータをデータベースに保存 」にチェックを入れると
「mwf_123」というpost typeで保存されます。
※123の部分はフォームの識別ID。
お問い合わせ件数を表示する
フォーム識別子は90だとすると:
$count_posts = wp_count_posts('mwf_90');
$count = $count_posts->publish;
お問い合わせデータを表示する
お問い合わせデータはカスタムポストタイプで登録されており、 自動返信メールに記述された変数({tracking_number}など)は、カスタムフィールドとして登録されているため、get_post_custom()で取得できます。
例えば、次のようなコードでフォーム識別子90のお問い合わせデータを引っ張ることができます。
$get_posts = get_posts( 'post_type=mwf_90&posts_per_page=-1');
if( $get_posts ):
foreach( $get_posts as $post ): setup_postdata( $post );
$cf = get_post_custom(); ?>
<table width="100%">
<tr>
<th width="25%">お問い合わせ番号</th>
<td><?= esc_html( $cf['tracking_number'][0] ); ?></td>
</tr>
<tr>
<th>件名</th>
<td><?= esc_html( $cf['title'][0] ); ?></td>
</tr>
<tr>
<th>お名前</th>
<td><?= esc_html( $cf['namae'][0] ); ?></td>
</tr>
<tr>
<th>お問い合わせ内容</th>
<td><?= esc_html( $cf['message'][0] ); ?></td>
</tr>
</table>
<?php
endforeach; wp_reset_postdata( $post );
endif;
WP-Membersと組み合わせて自分のお問い合わせ履歴をマイページで確認する機能をつけたりとか、特定のpost typeにMW Formを設置してそのお問い合わせ件数を集計したりとか、いろいろできそうです。
サンプルコード(自分用)
フォームと対象記事を紐づけるためのフック
各詳細記事に設置したMW Formがどの記事に設置されたか識別するためにフォームのinput type=”hidden”に自動入力させるためのフック。
function my_mwform_value( $value, $name ) {
global $post;
if ($name === 'target_id' ) {
return $post->ID;
}
if ($name === 'target_title' ) {
return $post->post_title;
}
return $value;
}
add_filter( 'mwform_value_mw-wp-form-90', 'my_mwform_value', 10, 2 );
記事のお問い合わせ件数を表示するショートコード
function sc_get_contactcount( $atts ) {
global $post;
$args = array(
'post_type' => 'mwf_90',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'target_id',
'value' => $post->ID,
'compare' => '=',
)
)
);
$get_posts = get_posts( $args );
$count = count( $get_posts );
$count = ( $count > 0 )? $count : '';
return $count;
}
add_shortcode( 'sc_get_contactcount', 'sc_get_contactcount' );
ユーザーごとのお問い合わせ一覧を表示するショートコード
固定ページ「マイページ」に埋め込む用
function sc_get_contactlog( $atts ) {
$current_user = wp_get_current_user();
if( $current_user ) {
$get_posts = get_posts( 'post_type=mwf_90&posts_per_page=100&author='.$current_user->ID );
ob_start();
if( $get_posts ) {
foreach( $get_posts as $get_post ):
$cf = get_post_custom( $get_post->ID ); ?>
<dl>
<dt><b>対象記事:</b><a href="<?= get_the_permalink( esc_html( $cf['target_id'][0] ) ); ?>"><?= esc_html( $cf['target_title'][0] ); ?></a></dt>
<dd><b>お問い合わせ内容:</b><?= esc_html( $cf['message'][0] ); ?></dd>
</dl><hr>
<?php
endforeach;
} else {
echo '<p>まだお問い合わせをしていません';
}
return ob_get_clean();
}
}
add_shortcode( 'sc_get_contactlog', 'sc_get_contactlog' );