Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get all post ids in wordpress

Tags:

php

wordpress

I want to get all published posts in wordpress, I tried

<div class="pppp" style="display:none">
<?php $post_ids = get_posts(array(
    'fields'        => 'ids', // Only get post IDs
));
var_dump($post_ids);
?>
</div>

but return only 5 last post ids.

 array(5) {   [0]=>   int(35102)   [1]=>   int(35097)   [2]=>   int(35094)   [3]=>   int(33281)   [4]=>   int(33279) } 

I want to know how can I get all postids of my wordpress sites?

like image 894
Yuseferi Avatar asked Sep 10 '16 13:09

Yuseferi


1 Answers

Reference: https://developer.wordpress.org/reference/functions/get_posts/#source

function get_posts( $args = null ) {
    $defaults = array(
        'numberposts' => 5,
        'category' => 0, 
        'orderby' => 'date',
        'order' => 'DESC', 
        'include' => array(),
        'exclude' => array(),
        'meta_key' => '',
        'meta_value' =>'',
        'post_type' => 'post',
        'suppress_filters' => true
    );
....
}

so you should add something to your array like

get_posts(array(
    'fields'          => 'ids', // Only get post IDs
    'posts_per_page'  => -1
));

Note: 'numberposts' and 'posts_per_page' can be used interchangeably.

like image 83
hakkikonu Avatar answered Sep 18 '22 12:09

hakkikonu