V
V
Vladislav Ignatiev2018-03-15 14:40:06
WordPress
Vladislav Ignatiev, 2018-03-15 14:40:06

Creating a loop within a loop with different $args?

I have a similar design:

$company_pname = wp_get_post_categories( $post->ID, array('fields' => 'all') );
  foreach( $company_pname as $cat_comp ){
  $args = array( 'post_type' => 'post-pharmacy', 'posts_per_page' => -1, 'name' => $cat_comp->slug); 
  $cat_comp = get_posts( $args );
  foreach( $cat_comp as $post ){ setup_postdata($post);
  $post_slug = $post->post_name;
  /* Мой контент, выводимые записи */
  }
}

How to rewrite it in the standard WP cycle, let's say:
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug'
        )
     )
);

$first_query = new WP_Query($args);

while ($first_query->have_posts()) :

    // initialization for $inner_args & backup the current global $post
    $inner_args = array(
      'post_type' => 'post-pharmacy', 
      'posts_per_page' => -1, 
      'name' => $first_query
    );
    $inner_query = new WP_Query($inner_args);

    while ($inner_query->have_posts()) :
       /* Мой контент, выводимые записи */
    endwhile;
    // restore the global $post from the previously created backup

endwhile;

It unfortunately doesn't work.
Why do I need this? Only in order to put pagination, if it is possible to add pagination for foreach, then please explain how?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Kozlov, 2018-03-15
@trampick

First, you make 1 request and save the required result, for example, into an array. After that don't forget to do wp_reset_postdata();
Then run foreach on the array and generate the necessary wp_query queries, remembering to use wp_reset_postdata();
This is the answer to your question. I would advise how to organize the algorithm itself in a different way. Since this one will be very resource intensive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question