• Resolved Shabir Virk

    (@sakuhetu)


    I am getting a problem when try to run Multiple Loops using Foreach

    The Original Code is

    $getpost = new WP_Query('posts_per_page=-1&femalesubcats=a-name-female-celebrities');
    if ($getpost->have_posts()) :
    	while ($getpost->have_posts()) : $getpost->the_post();
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags[] = $tag->term_id;
    			}
    		}
    	endwhile;
    endif;
    
    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);
    $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array', 'include'   => $tags_str ) );
    
    ?>
    
    <?php
    foreach ($tag as $tag_item) :
    echo $tag_item.'<br />';
    
    endforeach;

    You can see i created a custom Taxonomy named Female Sub Categories (femalesubcats) having these terms

    a-name-female-celebrities
    b-name-female-celebrities

    n-name-female-celebrities
    ..
    z-name-female-celebrities

    Totally A-Z Terms

    Now, i placed all of the terms in array and use a single foreach

    $termarray = array('a-name-female-celebrities', 'b-name-female-celebrities');
    foreach( $termarray as $terms )
    {
    $getpost = new WP_Query('posts_per_page=-1&femalesubcats='.$terms.'');
    if ($getpost->have_posts()) :
    	while ($getpost->have_posts()) : $getpost->the_post();
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags[] = $tag->term_id;
    			}
    		}
    	endwhile;
    endif;
    }
    
    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);
    $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array', 'include'   => $tags_str ) );
    
    ?>
    
    <?php
    foreach ($tag as $tag_item) :
    echo $tag_item.'<br />';
    
    endforeach;
    
    echo '--------------------';

    All of the terms are showing on one page.. But the problem is i need to write the “Seperator Line” after ending the A.

    But right now the line is showing after all the terms.. Means at the end of the page.

    Is there anything iam missing?
    Or is there any other easy way to do this.

    Waiting for Reply

Viewing 15 replies - 1 through 15 (of 26 total)
  • Try listing the terms like this:

    $current_letter = '';
    foreach ($tag as $tag_item) :
       preg_match('/>([^<]+)<\/a/', $tag_item, $matches );
       $this_letter = substr( $matches[1], 0, 1 );
       if ( $this_letter != $current_letter && $current_letter != '' ) :
          echo '--------------------<br />';
       endif;
       $current_letter = $this_letter;
       echo $tag_item.'<br />';
    endforeach;
    
    echo '--------------------';
    Thread Starter Shabir Virk

    (@sakuhetu)

    Thanks Its Working..

    But i don’t need to this actually.

    See this screen shot
    https://tinypic.com/view.php?pic=16huaad&s=8#.U4y6Sih7Qwk

    I need to do this, this is achieved by this code

    $getpost = new WP_Query('posts_per_page=-1&femalesubcats=a-name-female-celebrities');
    if ($getpost->have_posts()) :
    	while ($getpost->have_posts()) : $getpost->the_post();
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags[] = $tag->term_id;
    			}
    		}
    	endwhile;
    endif;
    
    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);
    $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array', 'include'   => $tags_str ) );
    
    ?>
    
    <?php
    foreach ($tag as $tag_item) :
    echo $tag_item.'<br />';
    
    endforeach;

    Now i have to place it over 26 times for a-z terms.

    But i want to do this by foreach as my code is always same

    But when i do this by my code i got this
    https://tinypic.com/view.php?pic=157ypnl&s=8#.U4y6Kih7Qwk

    You can clearly see the terms are mixed up.

    I just wanted the first one by doing foreach

    Sorry, I misunderstood what you wanted. I think this will do what you want:

    foreach ( range( 'a', 'z' ) as $first_letter ) :
       $getpost = new WP_Query("posts_per_page=-1&femalesubcats={$first_letter}-name-female-celebrities");
       if ($getpost->have_posts()) :
    		while ($getpost->have_posts()) : $getpost->the_post();
    		   $posttags = get_the_tags();
    		   if ($posttags) {
    				foreach($posttags as $tag) {
    				   $all_tags[] = $tag->term_id;
    				}
    		   }
    		endwhile;
       endif;
    
       $tags_arr = array_unique($all_tags);
       $tags_str = implode(",", $tags_arr);
       $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array', 'include'   => $tags_str ) );
    
       foreach ($tag as $tag_item) :
          echo $tag_item.'<br />';
       endforeach;
    endforeach;
    Thread Starter Shabir Virk

    (@sakuhetu)

    I Figured out something with this code

    $termarray = array('a-name-female-celebrities', 'b-name-female-celebrities', 'c-name-female-celebrities');
    foreach( $termarray as $terms )
    {
    $getpost = new WP_Query('posts_per_page=-1&femalesubcats='.$terms.'');
    if ($getpost->have_posts()) :
    	while ($getpost->have_posts()) : $getpost->the_post();
    		$posttags = get_the_tags();
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$all_tags[] = $tag->term_id;
    			}
    		}
    	endwhile;
    endif;
    
    $tags_arr = array_unique($all_tags);
    $tags_str = implode(",", $tags_arr);
    
    $tag = wp_tag_cloud( array('smallest' => 10, 'largest' => 10, 'orderby' => 'name', 'order' => 'ASC', 'number' => 0, 'format' => 'array','include'   => $tags_str ) );
    $tags_per_col = ceil(count($tag)/4); ?>
    <div class="panel panel-primary">
    <div class="panel-heading">
    <h3 class="panel-title bold">
    <?php
    $search = array('-');
    $replace = array(' ');
    $output = str_replace($search, $replace, $terms);
    echo '<span style="text-transform:capitalize">'.$output.'<span>';
    ?>
    </h3>
    </div>
    
    <div class="panel-body">
    
    <div class="tag_column">
    <?php  $count=0;
    foreach ($tag as $tag_item) :
    echo $tag_item.'<br />'; $count++;
    if ($count==$tags_per_col) {
    echo '</div><div class="tag_column">'; $count=0;
    }
    endforeach;
    echo '</div></div></div>'; }
    ?>
    
    </div>

    Now All the Terms are having the output i needed, but the problem is after the first foreach when it starts for next it is including the 1st terms too

    check the screenshot below
    https://tinypic.com/view.php?pic=1zea1qu&s=8

    I think i am missing something, Urgent help is required ??

    Just set the $all_tags array to an empty array before the first foreach.

    $all_tags = array();
    foreach( $termarray as $terms )
    {
    Thread Starter Shabir Virk

    (@sakuhetu)

    Could you please modify my code and paste it again.

    So I can check

    Just insert the $all_tags = array(); line before the first foreach.

    Thread Starter Shabir Virk

    (@sakuhetu)

    it is not working, showing the same result

    I request you to please paste your modified code here..

    Paste what you are testing.

    EDIT: Sorry, my mistake. Paste that line AFTER the foreach.

    Thread Starter Shabir Virk

    (@sakuhetu)

    Ohh I Got it.

    $all_tags = array();
    foreach ($tag as $tag_item)

    but you said for

    $all_tags = array();
    foreach( $termarray as $terms

    You told me the wrong foreach :p

    Thread Starter Shabir Virk

    (@sakuhetu)

    Thanks Dear

    Really you solved my biggest problem..

    Have a good day, god bless you.

    Thread Starter Shabir Virk

    (@sakuhetu)

    Hello vtxyzzy,

    Now I want to do some extra stuff with the code.

    It is printing all the tags But the problem is I have 3 categories

    Movies, Male Celebrities, Female Celebrities

    I assigned multiple TAGS to a single wallpapers example.

    “Harry Potter and the Deathly Hallows – Part 2” and “Matthew Lewis”

    This two are assigned to a Wallpapers

    Now when i query by my code it is showing both in the list.

    Is there any solution for it.

    Actually I just wanted to build a-z lists for all of Female Celebrties, Movies, and Male Celebrties.

    The Terms are stored In TAGS

    Is there any help on it?
    Thanks

    Please put the code you are using into a pastebin and post a link to it here.

    Thread Starter Shabir Virk

    (@sakuhetu)

    Hi,

    I soled it by a trick..
    $getpost = new WP_Query('posts_per_page=-1&category_name=female-celebrities&cat=-44,-1,-63');

    As you can see i m getting the posts by querying category_name and then removing the other categories by IDs

    So now i m getting the desired results.

    Just tell me, the code is ok or a bug.. i don’t want that in future the code stopped working.

    Also a little more help needed.

    You gave me this code few days back

    $current_letter = '';
    foreach ($tag as $tag_item) :
       preg_match('/>([^<]+)<\/a/', $tag_item, $matches );
       $this_letter = substr( $matches[1], 0, 1 );
       if ( $this_letter != $current_letter && $current_letter != '' ) :
          echo '--------------------<br />';
       endif;
       $current_letter = $this_letter;
       echo $tag_item.'<br />';
    endforeach;

    The problem is, it is echoing the line after the list.

    I need to Put a A-Z Index before the list, means no line.

    Here is the working code
    https:// xdesktopwallpapers.com/all-male-celebrities-wallpapers/

    Thanks

    The problem is, it is echoing the line after the list.

    I need to Put a A-Z Index before the list, means no line.

    Can you please explain the problem a bit more? If you can show an example that would help, something like this:

    Header – All Male Celebrities Wallpapers
    A tags
    ——
    B tags
    ——

    Also, I want to see the code, not the screen that results from it. Please put the current code into a pastebin and post a link to it.

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘Multiple Query_Posts Using Foreach’ is closed to new replies.