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;

ShoeLace1291
360
Comentarii
Toate răspunsurile la întrebare
2
0
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()

N00b
2.18K
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() ;
}
}

YoJey Thilipan
161
31 aug. 2018 10:42:40
Întrebări similare
4
răspunsuri