0

Как получить массив хранящий в себе ID всех постов(только постов, не страниц) в Wordpress ?

3 ответа 3

1
$post_IDS = array();

while ( have_posts() ) : the_post();
$post_ID = get_the_ID();
array_push($post_IDS, $post_ID);
endwhile;

echo $post_IDS[3];

так вроде бы работает.

0

Можно сделать запрос к базе, например:

 global $wpdb; 
  $sql="SELECT id FROM posts WHERE post_type='post'  ";
  $postsID = $wpdb->get_results($sql,ARRAY_A);
  print_r($postsID);
0

Попробуйте так

Здесь работает класс WP_Query, $args укажите нужные вам

$key_count = 0;

$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();

    $posts_ids_array[$key_count] = $post->ID;
    $key_count++ ;

wp_reset_postdata();
endwhile;
wp_reset_query();

Получите массив $posts_ids_array с id всех постов из выборки

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.