アプリ版:「スタンプのみでお礼する」機能のリリースについて

画像とカテゴリーを出力したいのですが、取得の条件を付ける方法がわかりません。
echoで出力することは分かるのですが、条件式を加えたい場合どうすればいいのでしょうか?
アドバイスお願い致します。

※取得条件
function set_other_data($post)
{
// アイキャッチIDを取得
$post_thumbnail_id = get_post_thumbnail_id($post);
// アイキャッチ画像の確認
if ($post_thumbnail_id) {
// 存在する
$image_src = wp_get_attachment_image_src($post_thumbnail_id);
// サムネイルの画像URLを設定
$post->thumbnail = $image_src[0];
} else {
// 存在しない
$post->thumbnail = 'noimage.jpg';
}
// カテゴリーIDを取得
$post->categories = wp_get_post_categories($post->ID);
// コメントテキスト
if (0 == $post->comment_count) {
// コメントなし
$post->comments = __('No Comments');
} else {
// コメントあり
$post->comments = $post->comment_count.'件のコメント';
}
// コメントリンク
$post->comments_link = get_comments_link($post->ID);
}

//<li></li>の中に出力する予定

<?php $search_query = get_search_query(); ?>
<?php global $wpdb; ?>
<?php
$sql = "
SELECT
post.post_title,
post.post_date,
post.post_excerpt,
post.comment_count,
attachment.guid AS thumbnail_url,
GROUP_CONCAT(category.name ORDER BY category.term_id) AS category_names,
GROUP_CONCAT(category.slug ORDER BY category.term_id) AS category_slugs
FROM
wp_posts AS post
LEFT JOIN (
SELECT
*
FROM
wp_postmeta
WHERE
meta_key = '_thumbnail_id'
) AS thumbnail
ON post.ID = thumbnail.post_id
LEFT JOIN wp_posts AS attachment
ON thumbnail.meta_value = attachment.ID
LEFT JOIN (
SELECT
sub_a.name,
sub_a.slug,
sub_c.object_id,
sub_a.term_id
FROM
wp_terms AS sub_a
LEFT JOIN wp_term_taxonomy AS sub_b
ON sub_a.term_id = sub_b.term_id
LEFT JOIN wp_term_relationships AS sub_c
ON sub_b.term_taxonomy_id = sub_c.term_taxonomy_id
WHERE sub_b.taxonomy = 'category'
) AS category
ON post.ID = category.object_id
WHERE
post.post_type = 'post'
AND
post.post_status = 'publish'
AND
(post.post_content LIKE %s OR post.post_title LIKE %s OR post.post_excerpt LIKE %s)
GROUP BY
post.ID
ORDER BY
post.post_date DESC
";
$query = $wpdb->prepare($sql, "%$search_query%", "%$search_query%", "%$search_query%");
$results = $wpdb->get_results($query);
<?php if ($results) : ?>
<?php foreach ($results as $result) : ?>
<li>
</li>
<?php endforeach; ?>

質問者からの補足コメント

  • アドバイスありがとうございますSQLを扱うのが初めてでして、画像の分岐についても教えて頂きたいのですが、どのように書けばいいのでしょうか?

    ※参考サイト https://atelierroi.com/tecnicalnote/wpdesign/wpi …

      補足日時:2022/05/02 13:13

A 回答 (2件)

>画像の分岐についても教えて頂きたいのですが、どのように書けばいいのでしょうか?


参考サイトを見た感じ、たんに本文(post_content)に正規表現マッチさせて、マッチ結果からサムネイルのURLを特定してるみたいです。
なんでsqlあまり関係ないかも。
    • good
    • 0
この回答へのお礼

ありがとうございます。

お礼日時:2022/05/06 23:14

>$post->categories = wp_get_post_categories($post->ID);


wp_get_post_categories()はこの場合、カテゴリーIDのリストを返すので、第2引数でカテゴリー名(names)をgetするように指定したほうが良いかと。
で、得られたカテゴリー名のリストで条件に該当するかチェックしては。
    • good
    • 0
この回答へのお礼

※参考にしたコード
function get_attachment_id_by_url( $url ) {
global $wpdb;
$sql = "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s";
preg_match( '/([^\/]+?)(-e\d+)?(-\d+x\d+)?(\.\w+)?$/', $url, $matches );
$post_name = $matches[1];
return ( int )$wpdb->get_var( $wpdb->prepare( $sql, $post_name ) );
}

function catch_that_image() {
global $post;
$first_img = '';
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
$first_img_src = $matches[1][0];
$attachment_id = get_attachment_id_by_url( $first_img_src );
$first_img = wp_get_attachment_image( $attachment_id, 'thumbnail', false, array( 'class' => 'archive-thumbnail' ) );
if( empty( $first_img ) ){
$first_img = '<img class="attachment_post_thumbnail" src="' . get_stylesheet_directory_uri() . '/images/no_image.png" alt="No image" />';
}
return $first_img;
}

お礼日時:2022/05/02 13:13

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!