Cum pot obține ID-ul unei postări dintr-o buclă WP_Query?

16 ian. 2016, 01:51:30
Vizualizări: 148K
Voturi: 16

Am o buclă WP_Query care obține postări de un anumit tip. Aceste postări au meta date personalizate, așa că am nevoie să pot obține ID-ul postării fără a-l afișa, pentru a putea afișa meta datele acelei postări. Cum pot obține ID-ul postării fără a-l afișa? Acesta este codul meu:

$menu_id = get_the_id();
        $category_args = array(
            'post_type' => 'category',
            'post_parent' => $menu_id
        );

        $menu_categories = new WP_Query($category_args);
        while($menu_categories->have_posts()) : $menu_categories->the_post(); 
            $category_id = ??????; ?>
        <h4><?php echo the_title(); ?></h4><?php 

            $dish_args = array(
                'post_type' => 'dish',
                'post_parent' => $category_id
            );
            $category_dishes = new WP_Query($dish_args);
            while($category_dishes->have_posts()) : $category_dishes->the_post(); 
                $dish_meta = get_post_meta(???????);?>
            <h6><?php echo the_title(); ?> - <?php echo $dish_meta[0]['price']; ?></h6>
            <p><?php echo the_content(); ?></p><?php
            endwhile;
        endwhile; 
3
Comentarii

$post_id = get_the_ID(); poate fi folosit în cadrul buclei. Această funcție returnează ID-ul postării curente gestionate de buclă.

N00b N00b
16 ian. 2016 03:49:46

@N00b ar trebui să postezi asta ca răspuns.

Pieter Goosen Pieter Goosen
16 ian. 2016 09:26:23

Încerci să obții categorii, sau ai un tip de postare personalizat numit "category"? Dacă este primul caz, atunci ar trebui să folosești get_categories(), dacă este al doilea caz, atunci ar trebui să citești acest articol: https://codex.wordpress.org/Reserved_Terms

Peter HvD Peter HvD
31 aug. 2018 15:26:55
Toate răspunsurile la întrebare 2
0
30

get_the_ID() poate fi folosit (doar) în cadrul buclei.

Această funcție returnează ID-ul postului curent procesat de buclă.


O puteți folosi individual dacă aveți nevoie de ea o singură dată:

$dish_meta = get_post_meta( get_the_ID(), 'dish_meta', true );

De asemenea, o puteți stoca într-o variabilă dacă aveți nevoie să o utilizați de mai multe ori:

$post_id = get_the_ID();

$dish_meta = get_post_meta( $post_id, 'dish_meta', true );

$drink_meta = get_post_meta( $post_id, 'drink_meta', true );

print_r( $post_id );

//etc

Referință: get_the_ID()

16 ian. 2016 10:24:53
0

Funcția get_the_ID() vă va oferi ID-ul postării..,

            $args = array(

                          's' => $_POST['search_text'],
                          'posts_per_page' => -1,
                          'post_type' => 'address'

                     );

            $query = new WP_Query( $args );

            if ( $query->have_posts() ) {

               while ( $query->have_posts() ) {

                    $query->the_post();

                    $address_post_id = get_the_ID() ;
               }
            }
31 aug. 2018 10:42:40