Support

Account

Home Forums General Issues Query posts by value (array?) and not by fieldname Reply To: Query posts by value (array?) and not by fieldname

  • I solved it. Maybe it could be a better code. But it works and i’m happy with it. 🙂

    <?php 
    			$posts = get_posts(array('meta_key'	=> 'haus'));
    			if( $posts ) {
    				$anzhaus = array();
    				foreach( $posts as $post ) {
    					setup_postdata( $post );
    					$anzhaus[] = get_field('haus'); 
    				}; //foreach( $posts as $post )
    				$uniquehaus = array_unique($anzhaus);			
    				foreach( $uniquehaus as $hpost ) {
    					setup_postdata( $hpost );
    					echo('<div class="hausbox"><div class="Haustitel">'.$hpost.'</div>');
    						$args = array('meta_query'	=> array('relation'		=> 'AND',	array('key'		=> 'geschoss'),	array('value'		=> $hpost)	));
    						$the_query = new WP_Query( $args );
    						if( $the_query->have_posts() ) {
    							$anzgeschoss = array();
    							while ( $the_query->have_posts() ) {
    								$the_query->the_post();
    								$anzgeschoss[] = get_field('geschoss');
    							};
    							$uniquegeschoss = array_unique($anzgeschoss);
    							foreach ($uniquegeschoss as $geschoss) {
    							echo'<div class="geschossbox"><div class="Geschosstitel">'.$geschoss.'</div>';																			
    								$wohnargs = array('meta_query'	=> array('relation'		=> 'AND',	array('value'		=> $geschoss),	array('value'		=> $hpost)	));																		
    								$wohn_query = new WP_Query( $wohnargs );
    								if( $wohn_query->have_posts() ) {																						
    									while( $wohn_query->have_posts() ) {
    										$wohn_query->the_post(); ?>
    										<div class="Wohnungsbox"> //here we go!!!
    											
    
    									</div>
    							<?php };
    									}; ?>
    								</div>	
    							<?php		};
    						};
    						wp_reset_query();	 // Restore global post data stomped by the_post(). 
    					echo('</div>'); //hausbox
    				} //foreach( $uniquehaus as $hpost )
    			}; //if( $posts )
    			wp_reset_query();	 // Restore global post data stomped by the_post().
     		?>