Support

Account

Home Forums Front-end Issues query based on current page

Solved

query based on current page

  • I have an event website with custom post types for events and for the business sponsoring the event. The business posts are a relational post in the event posts. I used the relational info from this site for the business name to show up in the events like this.

    
    <?php 
    $business = get_field('business_name');
    ?>
    <?php if( $business ): ?>
    	<a href="<?php echo get_permalink( $business->ID ); ?>">
    	        <?php echo get_the_title( $business->ID ); ?>
    	</a>
    <?php endif; ?>
    

    This works great. So I created a single-business.php to display the business post info. This also works fine but I also wanted to display all the events from that business as well but I can’t seem to get it working. I’ve tried something like this:

    
    $business1 = get_the_title( $ID );
    
    $args = array(
    	'post_type'  => 'event',
    	'orderby'    => array(
    	        'event_date' => 'ASC',
    		'time_start' => 'ASC'
    	),
    	'meta_query' => array(
    		array(
    			'key'     => 'business_name',
    			'value'   => $business1
    		)		
    	)
    	);
    	$the_query = new WP_Query( $args );
    

    This doesn’t seem to work, anyone have any ideas?

    Thanks,

    Matt

  • Since the business_name field is a relationship field, the values of that field is actually a serialized array of the Business post type IDs that are related to the event.

    See the single-location.php section of this page http://www.advancedcustomfields.com/resources/querying-relationship-fields/#single-location.php

  • I tried to copy what was on the page you linked but it still doesn’t return any events, just blank. The first “business” loop still works but there are no events displayed. Below is the full code.

    
    <?php while ( have_posts() ) : the_post(); ?>
    					
            <div class="large-12 medium-12 small-12 columns">
    		<h1><?php the_title( ); ?></h1> 
    	</div>
    						
    	<div class="large-9 medium-9 small-9 columns">
    		<ul class="large-block-grid-2 medium-block-grid-2">
    			<li>
    				<h6>
    				<a>"><?php the_field( 'business_website' ); ?></a>
    				</h6>
    			</li>
    			<li>
    				<h6>
    				<a>"><?php the_field( 'business_phone' ); ?></a>
    				</h6>
    			</li>
    		</ul>
    	</div>
    						
    	<?php 
    
    	$location = get_field('business_map');
    							
    	if( !empty($location) ):
            ?>
    		<div class="acf-map">
    		<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
    		</div>
    
    	<?php endif; ?>
    				
    	<?php
    			
    	$business_event = get_posts(array(
    		'post_type' => 'event',
    		'orderby'    => array(
    			'event_date' => 'ASC',
    			'time_start' => 'ASC'
    			),
    		'meta_query' => array(
    			array(
    			'key' => 'business_name', // name of custom field
    			'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    			'compare' => 'LIKE'
    			)
    		)
    		));
    	?>
    		
    	<?php if( $business_event ): ?>
    		
    		<ul class="large-block-grid-2 medium-block-grid-2">
    									
    		<?php foreach( $business_event as $business_event ): ?>
    										
    			<li>
    						
    				<?php include("event.php"); ?>
    						
    			</li>	
    												
    		<?php endforeach; ?>
    								
    		</ul>
    									
    	<?php endif; ?>
    												
    <?php endwhile; ?>
    
  • Is it outputting anything at all? Like a empty list?

    What do you get if you output the results of the query

    
    <?php
    			
    	$business_event = get_posts(array(
    		'post_type' => 'event',
    		'orderby'    => array(
    			'event_date' => 'ASC',
    			'time_start' => 'ASC'
    			),
    		'meta_query' => array(
    			array(
    			'key' => 'business_name', // name of custom field
    			'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    			'compare' => 'LIKE'
    			)
    		)
    		));
    
    // show results of above.
    echo '</pre>'; print_r($business_event); echo '</pre>';
    	?>
    
  • Yes it was outputting an empty list or nothing? Hard to tell…

    That code outputs “Array ()”

    Thanks,

    Matt

  • So either there is none of there is an error in the get_posts() arguments. After looking at the codex for get_posts I think it the wrong function to use in this case. Go back to WP_Query

    
    $args = array(
        'post_type'  => 'event',
        'meta_query' => array(
            array(
                'key' => 'business_name',
                'value' => '"'.get_the_ID().'"',
                'compare' => 'LIKE'
            )		
        )
     );
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            echo '<p>',get_the_title(),'</p>';
        }
    }
    wp_reset_postdata();
    
  • Had to tweak it some, something about the compare “like” wasn’t working. This seems to work…

    
    $args = array(
    	    'post_type'  => 'event',
    	    'meta_query' => array(
    	        array(
    	            'key' => 'business_name',
    	            'value' => get_the_ID()   
    	        )		
    	    )
    	    );
    

    Thanks!

    Matt

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘query based on current page’ is closed to new replies.