2017-05-31 15 views
0

WordPressの投稿名とカテゴリ名を使用して投稿IDを取得するにはどうすればよいですか?WordPressの投稿名とカテゴリ名を使って投稿IDを取得するには?

私はこのコードを使用していますが、それは私を助けません。

$post_slug = get_post_field($post_name, get_post()); 
$args = array(
'name'  => $post_slug, 
'category_name' => $pcat_name, 
'post_type' => 'post', 
'post_status' => 'publish', 
'numberposts' => 1 
); 
$my_posts = get_posts($args); 
if($my_posts) : 
$rid=$my_posts[0]->ID; 
endif; 

スラグは機能しません。

投稿名とカテゴリ名の両方で確認したいと思います。

答えて

0

あなたは支柱または任意の他のポストタイプを取得するためにget_page_by_title()(https://codex.wordpress.org/Function_Reference/get_page_by_title)を使用することができます。

$posts = get_page_by_title($post_name, OBJECT, 'post'); 
foreach($posts as $post){ 
    if(in_category($pcat_name, $post->ID)){ 
     echo $post->ID; 
    }; 
}; 
+1

これは私がものを期待されていません。投稿IDを使用して投稿IDが必要で、カテゴリ名もチェックします。 –

関連する問題