Combine Arrays in foreach Loop

I recently built a website with a large featured banner on their home page, and events page. Banners information was held in a Custom Post Type, and Events were created by The Events Calendar plugin.

The client wanted to combine these two items to show all of the banners on the Home page, to more readily promote their events.

I originally thought about running two separate loops for the content … but this was a messy approach because they could have up to five of each content type, and that means it could be five slides before seeing content from the other section.

I decided to combine the arrays as follows:

$banners = get_posts(array(
	'post_type' => 'banner',
	'posts_per_page' => 5,
$events = tribe_get_events(array(
	'posts_per_page' => 5,
	'start_date' => current_time( 'Y-m-d' ),
	'eventDisplay' => 'list', // ignores repeating events 
	'tribe_events_cat' => 'promoted-event' // specific category for banner display

$both_arrays = array_merge((array)$banners, (array)$events);

foreach($both_arrays as $post):

// banner content


Source for array_merge with example.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.