LoginSignup
31
32

More than 5 years have passed since last update.

WordPressで指定したカスタムフィールドが存在しない場合の投稿一覧を取得する方法

Last updated at Posted at 2015-02-25

逆の記事はあるけど・・・

指定したカスタムフィールドが「存在する」場合の一覧を取得する方法についてはいくつかの記事がある。

$posts = get_posts(array(
  'post_type' => 'post',
  'meta_key' => 'key_name',
  'meta_value' => 'null',
  'meta_compare' => '!='
));

また、指定したカスタムフィールドの値が空の場合の記事もある。

$posts = query_posts(array(
  'post_type' => 'post',
  'meta_key' => 'key_name',
  'meta_value' => ' ',
  'meta_compare' => '='
));

でも指定したカスタムフィールドが「存在しない」場合について書かれてる記事が見つからない。。

答えは海外のサイトにあり

日本語での記事はないかーと諦めて「wordpress get_posts meta_key empty」でググったら出てきました。

$posts = get_posts(array(
  'post_type' => 'post',
  'meta_key' => 'key_name',
  'meta_compare' => 'NOT EXISTS'
));

上記のコードで無事に「指定したカスタムフィールドが存在しない記事の一覧」を取得することに成功しました。

めでたしめでたし。

その他

get_postsquery_posts について

get_postsquery_posts で使えるパラメーターの一覧

31
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
32