Ich habe eine Frage wie diese, ich werde die ID's für das Produkt bekommen. Das funktioniert gut:
function ids(){
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_key' => 'wppl_is_dax',
'meta_value' => '1'
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ): while( $the_query->have_posts() ) : $the_query->the_post();
global $product;
return $product->get_id();
endwhile; endif; wp_reset_query();
}
Aber jetzt möchte ich die Ausgabe der obigen Abfrage im Folgenden verwenden
function tester2(){
$targetted_products = array(/* the ids from above function- ids()*/);
}
Ich erhalte nur eine ID, wenn ich $ targetted_products = array (ids ()) verwende.
Ihre Funktion gibt $product->get_id();
zurück, stattdessen sollten Sie diese Werte in einem Array speichern und am Ende dieses Arrays zurückgeben.
function ids(){
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_key' => 'wppl_is_dax',
'meta_value' => '1'
);
// query
$the_query = new WP_Query( $args );
$allIds = array();
if( $the_query->have_posts() ): while( $the_query->have_posts() ) : $the_query->the_post();
global $product;
array_Push($allIds,$product->get_id());
endwhile; endif; wp_reset_query();
return $allIds;
}
Wenn Sie nur IDs benötigen, verbraucht die Abfrage viel weniger Speicher, wenn Sie den Parameter fields
verwenden, um nur das eine Feld in einem Array zurückzugewinnen:
function ids(){
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_key' => 'wppl_is_dax',
'meta_value' => '1'
'fields' => 'ids'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ){
return $the_query->posts;
}
return false;
}
function ids(){
$args = array(
'numberposts' => -1,
'post_type' => 'product',
'meta_key' => 'wppl_is_dax',
'meta_value' => '1'
);
// query
$the_query = new WP_Query( $args );
$post_ids = [];
if( $the_query->have_posts() ):
$post_ids = wp_list_pluck( $the_query->posts, 'ID' );
endif;
wp_reset_query();
return $post_ids;
}
lesen Sie mehr https://codex.wordpress.org/Function_Reference/wp_list_pluck