Getting an array out of WPQuery
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I have a query like this i will get the id's for product. This works fine:
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();
But now i want to use the output from the above query in the below
function tester2()
$targetted_products = array(/* the ids from above function- ids()*/);
I am only getting one id if i use
$targetted_products =array(ids());
wp-query array
add a comment |Â
up vote
3
down vote
favorite
I have a query like this i will get the id's for product. This works fine:
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();
But now i want to use the output from the above query in the below
function tester2()
$targetted_products = array(/* the ids from above function- ids()*/);
I am only getting one id if i use
$targetted_products =array(ids());
wp-query array
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a query like this i will get the id's for product. This works fine:
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();
But now i want to use the output from the above query in the below
function tester2()
$targetted_products = array(/* the ids from above function- ids()*/);
I am only getting one id if i use
$targetted_products =array(ids());
wp-query array
I have a query like this i will get the id's for product. This works fine:
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();
But now i want to use the output from the above query in the below
function tester2()
$targetted_products = array(/* the ids from above function- ids()*/);
I am only getting one id if i use
$targetted_products =array(ids());
wp-query array
asked Aug 13 at 20:43
Latheesh V M Villa
299113
299113
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Your function returns $product->get_id();
, instead of that, you should save those values into an array and at the end return that array.
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;
add a comment |Â
up vote
7
down vote
If you only want IDs, the query will consume much less memory if you use the fields
parameter to just get that one field back in an array:
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;
1
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
add a comment |Â
up vote
1
down vote
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;
read more https://codex.wordpress.org/Function_Reference/wp_list_pluck
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your function returns $product->get_id();
, instead of that, you should save those values into an array and at the end return that array.
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;
add a comment |Â
up vote
1
down vote
accepted
Your function returns $product->get_id();
, instead of that, you should save those values into an array and at the end return that array.
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;
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your function returns $product->get_id();
, instead of that, you should save those values into an array and at the end return that array.
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;
Your function returns $product->get_id();
, instead of that, you should save those values into an array and at the end return that array.
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;
answered Aug 13 at 20:51
Castiblanco
1,73221020
1,73221020
add a comment |Â
add a comment |Â
up vote
7
down vote
If you only want IDs, the query will consume much less memory if you use the fields
parameter to just get that one field back in an array:
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;
1
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
add a comment |Â
up vote
7
down vote
If you only want IDs, the query will consume much less memory if you use the fields
parameter to just get that one field back in an array:
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;
1
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
add a comment |Â
up vote
7
down vote
up vote
7
down vote
If you only want IDs, the query will consume much less memory if you use the fields
parameter to just get that one field back in an array:
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;
If you only want IDs, the query will consume much less memory if you use the fields
parameter to just get that one field back in an array:
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;
edited Aug 14 at 0:41
answered Aug 14 at 0:31
Milo
63.6k272108
63.6k272108
1
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
add a comment |Â
1
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
1
1
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
This is better than the accepted answer.
â Fayaz
Aug 14 at 2:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
Agreed! Thanks Milo.
â Castiblanco
Aug 14 at 16:38
add a comment |Â
up vote
1
down vote
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;
read more https://codex.wordpress.org/Function_Reference/wp_list_pluck
add a comment |Â
up vote
1
down vote
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;
read more https://codex.wordpress.org/Function_Reference/wp_list_pluck
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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;
read more https://codex.wordpress.org/Function_Reference/wp_list_pluck
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;
read more https://codex.wordpress.org/Function_Reference/wp_list_pluck
answered Aug 13 at 21:38
Valeriy Vasiliev
344
344
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f311341%2fgetting-an-array-out-of-wpquery%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password