Getting an array out of WPQuery

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite
1












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());







share|improve this question


























    up vote
    3
    down vote

    favorite
    1












    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());







    share|improve this question






















      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      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());







      share|improve this question












      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());









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 13 at 20:43









      Latheesh V M Villa

      299113




      299113




















          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;






          share|improve this answer



























            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;






            share|improve this answer


















            • 1




              This is better than the accepted answer.
              – Fayaz
              Aug 14 at 2:38










            • Agreed! Thanks Milo.
              – Castiblanco
              Aug 14 at 16:38

















            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






            share|improve this answer




















              Your Answer







              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "110"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              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






























              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;






              share|improve this answer
























                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;






                share|improve this answer






















                  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;






                  share|improve this answer












                  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;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 13 at 20:51









                  Castiblanco

                  1,73221020




                  1,73221020






















                      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;






                      share|improve this answer


















                      • 1




                        This is better than the accepted answer.
                        – Fayaz
                        Aug 14 at 2:38










                      • Agreed! Thanks Milo.
                        – Castiblanco
                        Aug 14 at 16:38














                      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;






                      share|improve this answer


















                      • 1




                        This is better than the accepted answer.
                        – Fayaz
                        Aug 14 at 2:38










                      • Agreed! Thanks Milo.
                        – Castiblanco
                        Aug 14 at 16:38












                      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;






                      share|improve this answer














                      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;







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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












                      • 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










                      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






                      share|improve this answer
























                        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






                        share|improve this answer






















                          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






                          share|improve this answer












                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 13 at 21:38









                          Valeriy Vasiliev

                          344




                          344



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              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













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              Installing NextGIS Connect into QGIS 3?

                              One-line joke