Obține toate ID-urile articolelor dintr-o categorie

4 nov. 2012, 03:11:17
Vizualizări: 45.2K
Voturi: 7

Problemă: Am nevoie să obțin un array cu ID-urile articolelor dintr-o categorie dată, dacă categoria respectivă conține articole. Acest lucru va fi folosit într-o pagină de opțiuni a unui plugin.

Până acum am:

$posts = get_posts(array('numberposts' => 10000, 'category' => 5));

Dar mă chinui să găsesc cum pot obține un array care să conțină doar ID-ul fiecărui articol din acea categorie.

Aveți idei? Mulțumesc

0
Toate răspunsurile la întrebare 2
0
16

Lucrul important de reținut despre get_posts este că utilizează intern un obiect WP_Query. Codul sursă al get_posts:

<?php
/**
 * Retrieve list of latest posts or posts matching criteria.
 *
 * The defaults are as follows:
 *     'numberposts' - Default is 5. Total number of posts to retrieve.
 *     'offset' - Default is 0. See {@link WP_Query::query()} for more.
 *     'category' - What category to pull the posts from.
 *     'orderby' - Default is 'post_date'. How to order the posts.
 *     'order' - Default is 'DESC'. The order to retrieve the posts.
 *     'include' - See {@link WP_Query::query()} for more.
 *     'exclude' - See {@link WP_Query::query()} for more.
 *     'meta_key' - See {@link WP_Query::query()} for more.
 *     'meta_value' - See {@link WP_Query::query()} for more.
 *     'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
 *     'post_parent' - The parent of the post or post type.
 *     'post_status' - Default is 'publish'. Post status to retrieve.
 *
 * @since 1.2.0
 * @uses $wpdb
 * @uses WP_Query::query() See for more default arguments and information.
 * @link http://codex.wordpress.org/Template_Tags/get_posts
 *
 * @param array $args Optional. Overrides defaults.
 * @return array List of posts.
 */
function get_posts($args = null) {
    $defaults = array(
        'numberposts' => 5, 'offset' => 0,
        'category' => 0, 'orderby' => 'post_date',
        'order' => 'DESC', 'include' => array(),
        'exclude' => array(), 'meta_key' => '',
        'meta_value' =>'', 'post_type' => 'post',
        'suppress_filters' => true
    );

    $r = wp_parse_args( $args, $defaults );
    if ( empty( $r['post_status'] ) )
        $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
    if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
        $r['posts_per_page'] = $r['numberposts'];
    if ( ! empty($r['category']) )
        $r['cat'] = $r['category'];
    if ( ! empty($r['include']) ) {
        $incposts = wp_parse_id_list( $r['include'] );
        $r['posts_per_page'] = count($incposts);  // only the number of posts included
        $r['post__in'] = $incposts;
    } elseif ( ! empty($r['exclude']) )
        $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );

    $r['ignore_sticky_posts'] = true;
    $r['no_found_rows'] = true;

    $get_posts = new WP_Query;
    return $get_posts->query($r);

}

Ceea ce înseamnă, desigur, că puteți folosi oricare dintre aceleași argumente pe care le acceptă WP_Query. Acest lucru include și parametrii legați de câmpuri.

Pentru a obține un array doar cu ID-uri, ar trebui să faceți ceva de genul:

<?php
$post_ids = get_posts(array(
    'numberposts'   => -1, // obține toate articolele.
    'tax_query'     => array(
        array(
            'taxonomy'  => 'category',
            'field'     => 'id',
            'terms'     => 5,
        ),
    ),
    'fields'        => 'ids', // Obține doar ID-urile articolelor
));

Sau puteți încapsula acest lucru într-o funcție pentru mai multă flexibilitate.

<?php
function wpse71471_get_post_ids($cat, $taxonomy='category')
{
    return get_posts(array(
        'numberposts'   => -1, // obține toate articolele.
        'tax_query'     => array(
            array(
                'taxonomy'  => $taxonomy,
                'field'     => 'id',
                'terms'     => is_array($cat) ? $cat : array($cat),
            ),
        ),
        'fields'        => 'ids', // obține doar ID-urile articolelor.
    ));
}
4 nov. 2012 03:30:04
2

Vă rugăm să consultați următorul articol: Obțineți ID-urile postărilor din WP_Query?

Ar trebui să utilizați: wp_list_pluck

Exemplu:

$ids = get_posts( array(
    'post_type' => 'contact',
    'pages_per_post' => -1,
) );

var_dump(wp_list_pluck( $ids, 'ID' ));
27 oct. 2015 03:29:23
Comentarii

Acest lucru este încă foarte, foarte costisitor. Returnezi întregul obiect post pentru fiecare articol. Dacă ai peste 1000 de articole, interogarea ta poate expira. Cea mai ușoară și cea mai bună soluție este să setezi parametrul fields la ids, acest lucru va returna doar ID-uri. Acest lucru economisește o SUMĂ ENORMĂ de apeluri la baza de date și timp necesar pentru executarea interogării.

Pieter Goosen Pieter Goosen
27 oct. 2015 06:21:07

Răspunsul de la @s_ha_dum din postarea legată este metoda corectă dacă dorești doar ID-uri de la postare.

Pieter Goosen Pieter Goosen
27 oct. 2015 06:22:29