LoginSignup
2
2

More than 5 years have passed since last update.

get_posts('p=1');で記事を取得できない時のメモ

Last updated at Posted at 2015-05-29

あるはずの記事が出てこない

指定したIDの投稿を取得しようとget_postsを使用して以下の様に書いたのだけど、
何故か$postsには空の配列が入るのみで取得できない。

before.php
<?php 
$arg =
    array(
        'p' => 1
    );
$posts = get_posts( $arg );
?>

post_typeの指定が必須

どうやらpost_typeの指定は必須らしい。
記事ならpost、固定ページならpage、カスタム投稿タイプならそのIDをちゃんと書こう。

after.php
<?php 
$arg =
    array(
        'p' => 1,
        'post_type' => 'post' //これが要る
    );
$posts = get_posts( $arg );
?>
2
2
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
2
2