質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

223閲覧

WORDPRESSのの一覧表示で、「投稿のカスタムフィールドの値」と「ユーザーのカスタムフィールドの値(Advanced Custom Fieldsで設定したもの)」で絞り込んで表示したい

mainichi_sn

総合スコア15

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2019/03/25 05:11

前提・実現したいこと

WORDPRESSの投稿ページでの記事一覧表示(関連投稿の表示)で、
「投稿のカスタムフィールドの値」と「ユーザーのカスタムフィールドの値(Advanced Custom Fieldsで設定したもの)」で絞り込んで表示したいのですが、うまくできず、ヒントなどあれば教えていただきたいです。

現状、配列の結合によるエラーがでます。
$myPosts = var_dump($item_args + $users_args);

$myPosts = $item_args ;
にすれば、ユーザーカスタムフィールド以外での絞り込みはできました。

発生している問題・エラーメッセージ

array(5) { [0]=> object(WP_Post)#6797 (24) { ["ID"]=> int(280) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2019-03-24 22:56:20" ["post_date_gmt"]=> string(19) "2019-03-24 13:56:20" ["post_content"]=> string(0) "" ["post_title"]=> string(27) "改登録サンプル商品" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(81) "%e6%94%b9%e7%99%bb%e9%8c%b2%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab%e5%95%86%e5%93%81" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2019-03-24 22:56:20" ["post_modified_gmt"]=> string(19) "2019-03-24 13:56:20" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(57) "https://leather-makers.com/?post_type=products&p=280" ["menu_order"]=> int(0) ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#7170 (24) { ["ID"]=> int(267) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2019-03-23 18:42:02" ["post_date_gmt"]=> string(19) "2019-03-23 09:42:02" ["post_content"]=> string(0) "" ["post_title"]=> string(60) "テストユーザーのブラウンなサンプルの商品" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(198) "%e3%83%86%e3%82%b9%e3%83%88%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e3%83%94%e3%83%b3%e3%82%ad%e3%83%bc%e3%83%91%e3%82%b9%e3%82%b1%e3%83%bc%e3%82%b9%e7%9a%84%e3%81%aa%e3%82%b5%e3%83%b3%e3%83%97" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2019-03-24 22:39:00" ["post_modified_gmt"]=> string(19) "2019-03-24 13:39:00" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(57) "https://leather-makers.com/?post_type=products&p=267" ["menu_order"]=> int(0) ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [2]=> object(WP_Post)#7169 (24) { ["ID"]=> int(278) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2019-03-24 22:47:09" ["post_date_gmt"]=> string(19) "2019-03-24 13:47:09" ["post_content"]=> string(0) "" ["post_title"]=> string(57) "テストユーザーのカテゴリー長財布の商品" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(171) "%e3%83%86%e3%82%b9%e3%83%88%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e3%83%bc%e9%95%b7%e8%b2%a1%e5%b8%83%e3%81%ae%e5%95%86%e5%93%81" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2019-03-24 23:26:53" ["post_modified_gmt"]=> string(19) "2019-03-24 14:26:53" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(57) "https://leather-makers.com/?post_type=products&p=278" ["menu_order"]=> int(0) ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [3]=> object(WP_Post)#6917 (24) { ["ID"]=> int(271) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2019-03-24 22:42:59" ["post_date_gmt"]=> string(19) "2019-03-24 13:42:59" ["post_content"]=> string(0) "" ["post_title"]=> string(63) "テストユーザーのピンクピンクサンプルの商品" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(189) "%e3%83%86%e3%82%b9%e3%83%88%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e3%83%94%e3%83%b3%e3%82%af%e3%83%94%e3%83%b3%e3%82%af%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab%e3%81%ae%e5%95%86%e5%93%81" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2019-03-24 22:42:59" ["post_modified_gmt"]=> string(19) "2019-03-24 13:42:59" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(57) "https://leather-makers.com/?post_type=products&p=271" ["menu_order"]=> int(0) ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [4]=> object(WP_Post)#6916 (24) { ["ID"]=> int(273) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2019-03-24 22:44:39" ["post_date_gmt"]=> string(19) "2019-03-24 13:44:39" ["post_content"]=> string(0) "" ["post_title"]=> string(69) "テストユーザーのイエローモンキーサンプルの商品" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(198) "%e3%83%86%e3%82%b9%e3%83%88%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e3%82%a4%e3%82%a8%e3%83%ad%e3%83%bc%e3%83%a2%e3%83%b3%e3%82%ad%e3%83%bc%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab%e3%81%ae%e5%95%86" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2019-03-24 22:44:39" ["post_modified_gmt"]=> string(19) "2019-03-24 13:44:39" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(57) "https://leather-makers.com/?post_type=products&p=273" ["menu_order"]=> int(0) ["post_type"]=> string(8) "products" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } }

該当のソースコード

php

1global $post; 2 3$args = array( 4'numberposts' => 5, //表示件数 5'post_type' => 'products', //カスタム投稿タイプ名 6'author__not_in' => array( $author_id ), //表示作成者を除外 7'orderby' => 'rand',//ランダム表示 8); 9$item_args = get_posts($args); 10 11$users = array( 12'meta_query' => array( array( 13'key' => '関連商品の表示', //ユーザーカスタムフィールドのキー 14'value' => '利用する', //ユーザーカスタムフィールドの値 15'compare' => 'LIKE', 16 17) ), 18); 19$users_args = get_users( $users ); 20 21$myPosts = var_dump($item_args + $users_args); 22 23if($myPosts) : 24foreach($myPosts as $post) : setup_postdata($post); 25 26//ループ表示部分 27 28endforeach; 29endif; 30wp_reset_postdata();

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

$myPosts = var_dump($item_args + $users_args);

は、何がしたいのでしょうか?

var_dump - PHPマニュアル には、

var_dump — 変数に関する情報をダンプする

とあります。

発生している問題・エラーメッセージ に記載されている内容は、var_dump($item_args + $users_args) を実行して画面に出力された 配列の値が表示されていると思われます。


プログラム全体を見た感じでは、

array_merge - PHPマニュアル

array_merge — ひとつまたは複数の配列をマージする

と間違えて、使用しているのでしょうか?

投稿2019/03/27 06:07

CHERRY

総合スコア25171

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mainichi_sn

2019/03/27 10:07

ご回答ありがとうございます! PHP初心者で、var_dumpで配列を結合できるものと勘違いして書いておりました。 array_mergeに変更したところエラーでなくなりました! ただ、$usersで指定した条件がきいておらず引き続き悩んでおります。
CHERRY

2019/03/27 10:22

質問の内容だけでは、何がしたいのかわからなかったので、回答では触れなかったのですが、 > $users_args = get_users( $users ); で、$users_args に入るのは、WP_User オブジェクトなので、ユーザーの情報になりますが、何がしたいのでしょうか?
mainichi_sn

2019/03/29 02:38

ご返信ありがとうございます! ユーザー情報にカスタムフィールドで設定した値('key' => '関連商品の表示', 'value' => '利用する',)で 「利用している」をセレクトしているユーザーの投稿のみ表示 と指定したいのですが どう記述してよいかわからず、あのようなコードを試した次第です。
CHERRY

2019/03/29 02:52

「関連商品の表示を利用する 」になっている ユーザーが投稿したメッセージだけをを表示したいということですか?
mainichi_sn

2019/03/29 02:54

ご返信ありがとうございます! そうです! $item_args = get_posts($args); の指定とどう合わせたらよいかわからず..
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問