WordpressでSQL文を使ってカスタムフィールドを表示させたいです。
【理想の表示】
投稿の一覧ページ
タイトル:
コンテンツ内容:
抜粋:
カスタムフィールド1:
カスタムフィールド2:
カスタムフィールド3:
タイトル:
コンテンツ内容:
抜粋:
カスタムフィールド1:
カスタムフィールド2:
カスタムフィールド3:
タイトル:
コンテンツ内容:
抜粋:
カスタムフィールド1:
カスタムフィールド2:
カスタムフィールド3:
タイトル:
コンテンツ内容:
抜粋:
カスタムフィールド1:
カスタムフィールド2:
カスタムフィールド3:
【問題】
カスタムフィールドだけが全て同じ内容表示されます。
下記がソースです。
<?php $mydb = new wpdb('user','password','databasename','hostname'); $result = $mydb->get_results(" SELECT post_title, id, guid, post_excerpt FROM wp_posts WHERE post_type = 'voice' AND post_status = 'publish' /* かつ公開済の記事 */ ORDER BY post_date DESC /* 新しい順に並び替え */"); $thumb = $mydb->get_var("SELECT ID FROM $wpdb->posts where post_parent = '$value' and post_type = 'attachment'"); $thumbnail = wp_get_attachment_image_src($thumb); $meta_key = 'letter_short'; $letter_short = $mydb->get_var( $mydb->prepare( " SELECT meta_value FROM wp_postmeta WHERE meta_key = %s ", $meta_key ) ); $meta_key = 'letter_all'; $letter_all = $mydb->get_var( $mydb->prepare( " SELECT meta_value FROM wp_postmeta WHERE meta_key = %s ", $meta_key ) ); $meta_key = 'description'; $description = $mydb->get_var( $mydb->prepare( " SELECT meta_value FROM wp_postmeta WHERE meta_key = %s ", $meta_key ) ); $meta_key = 'comment'; $comment = $mydb->get_results(" SELECT post_title, post_content, guid, ID, meta_value /* 値を取り出す */ FROM wp_posts /* テーブルを指定 */ INNER JOIN wp_postmeta AS m1 ON m1.post_id = wp_posts.ID /* テーブルを結合-カスタムフィールド */ AND m1.meta_key = 'comment' /* テーブルを結合しカスタムフィールドの値を指定 */ ", $meta_key); foreach ($result as $obj) : echo "<div id='post-".$obj->id."' class='post type-post status-publish format-standard hentry category-sonota'>"; echo "<a class='relatedimg' style='text-decoration:none;' href=\"". get_permalink($value+0) . "\" title=\"". $zg_title_out->post_title . "\"><img src='" .$thumbnail[0]. "' alt='Release Image' />" . $zg_title_out->post_title . "</a>"; echo "<h3>"; echo "<a href=".$obj->guid.">"; echo "$obj->post_title"; echo "</a>"; echo "</h3>"; echo "<p>$obj->post_excerpt</p>"; echo "<p>カスタムフィールド1: {$letter_short}</p>"; echo "<p>カスタムフィールド2 : {$letter_all}</p>"; echo "<p>カスタムフィールド3: {$description}</p>"; echo "<p>コメント: {$comment}</p>"; echo "</div>"; endforeach; ?>
回答2件
あなたの回答
tips
プレビュー