Title: String not returning from wp_query loop
Last modified: August 22, 2016

---

# String not returning from wp_query loop

 *  Resolved [Eric](https://wordpress.org/support/users/eklemen2/)
 * (@eklemen2)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/string-not-returning-from-wp_query-loop/)
 * I am trying to return a string built via a wp_query loop. I’ve simplified the
   code below for this post. In this example, “test” only returns if I comment out
   the $merged_presentation_events if/while loop. Ideally, I’d like to see the string
   concatenate an unordered list.
 * Thanks in advance for the help!
 *     ```
       <?php 
   
       function the_merged_presentation_events($bool_upcoming){
   
       	if ( $bool_upcoming == TRUE){
       		$order = 'asc';
       	} else {
       		$order = 'desc';
       	}
   
       	// Get webinars and conferences presentation ids person has made
       	$webinar_ids			= get_presentation_event_ids( 'webinars', $bool_upcoming );
       	$conference_ids		= get_presentation_event_ids( 'conferences', $bool_upcoming );
   
       	// Merge webinars and conferences presentation ids into a single array
       	$merged_presentation_event_ids = array_merge( $webinar_ids, $conference_ids );
   
       	echo '<pre>';
       	var_dump($merged_presentation_event_ids);
       	echo '</pre>';
   
       	// Check if merged array contains any IDs
       	if ( empty($merged_presentation_event_ids) ){
       		// merged array is empty, don't pass an empty array to post__in or all posts will be returned. See https://core.trac.wordpress.org/ticket/28099
       		return false;
       	}
   
       	// Query the merged array of presentation ids, sorting by event start date
       	$args_merged_presentation_events = array(
       		'post_type'				=> array('conferences', 'webinars'), // need to include or all posts are returned (even with ids defined)
       		'posts_per_page'	=> 5,
       		'post__in'				=> $merged_presentation_event_ids,
       		'meta_key'				=> 'event_start_date',
       		'order'						=> $order,
       		'orderby'					=> 'meta_value_num'
       	);
   
       	$merged_presentation_events = new WP_Query( $args_merged_presentation_events );
   
       	$str = '<ul>';
   
       	if( $merged_presentation_events->have_posts() ){
       		while( $merged_presentation_events->have_posts() ){
       			$merged_presentation_events->the_post();
       			$str .= '<li>' . get_the_title() . '</li>';
       		}
       	}
   
       	$str .= '</ul>';
   
       	return $str;
   
       	wp_reset_postdata();
   
       }
   
        ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [Eric](https://wordpress.org/support/users/eklemen2/)
 * (@eklemen2)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/string-not-returning-from-wp_query-loop/#post-5688409)
 * I discovered my error wasn’t in the loop itself, but in how I was calling the
   function. Here’s how I correctly called the function:
 *     ```
       <?php
       $upcoming_events = the_merged_presentation_events(true);
       if( $upcoming_events !== false ){
       	echo '<h3>Upcoming Presentations</h3>';
       	echo $upcoming_events;
       }
       >
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘String not returning from wp_query loop’ is closed to new replies.

## Tags

 * [return](https://wordpress.org/support/topic-tag/return/)
 * [string](https://wordpress.org/support/topic-tag/string/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Eric](https://wordpress.org/support/users/eklemen2/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/string-not-returning-from-wp_query-loop/#post-5688409)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
