How to Change Which Campaigns Show on the Homepage

By default, the “Homepage” template in Reach will show a grid of campaigns below the page content. This grid will show the most recently created campaigns on your site. It will show as many campaigns as you have set for the “Blog pages show at most” setting under Settings > Reading in your WordPress dashboard (defaults to 10).

With the child theme, you can change which campaigns are shown here, as well as how many.

1. Create a custom homepage template

In your child theme, create a folder called “page-templates” and inside it, add a new file called “homepage.php”. In other words, this file should be at:

reach-child-theme/page-templates/homepage.php

Next, copy into this file the contents of the matching file in the main Reach theme:

reach/page-templates/homepage.php

Now there is one small change you need to make to the homepage.php file in your child theme. Locate this line:

<?php get_template_part( 'partials/campaign', 'grid' ) ?>

Replace it with this:

<?php get_template_part( 'partials/campaign', 'grid-homepage' ) ?>

2. Create a custom campaign grid template

Next, create a new file in your child theme at:

reach-child-theme/partials/campaign-grid-homepage.php

Into this file, copy the contents of the following template in the main Reach theme:

reach/partials/campaign-grid.php

The final step is to modify the PHP function that gets the campaigns. Look for this line:

$campaigns = Charitable_Campaigns::query();

This line of code uses the Charitable_Campaigns class in Charitable to fetch a collection of campaigns. As noted above, it will return the most recently created campaigns by default and will use the “Blog pages show at most” setting for the number.

By changing this one line, you can alter which campaigns are shown by default. Here are a few examples:

Change how many campaigns are shown

$campaigns = Charitable_Campaigns::query( array(
        'posts_per_page' => 15
) );

Only show current, active campaigns

$campaigns = Charitable_Campaigns::query( array(
	'meta_query' => array(
		'relation' => 'OR',
		array(
			'key'       => '_campaign_end_date',
			'value'     => date( 'Y-m-d H:i:s' ),
			'compare'   => '>=',
			'type'      => 'datetime',
		),
		array(
			'key'       => '_campaign_end_date',
			'value'     => 0,
			'compare'   => '=',
		),
	)
) );

Only show campaigns that have finished

$campaigns = Charitable_Campaigns::query( array(
	'meta_query' => array(
		array(
			'key'       => '_campaign_end_date',
			'value'     => date( 'Y-m-d H:i:s' ),
			'compare'   => '<',
			'type'      => 'datetime',
		),
	)
) );

Only show campaigns in a particular category

$campaigns = Charitable_Campaigns::query( array(
	'tax_query' => array(
		array(
			'taxonomy'  => 'campaign_category',
			'field'     => 'slug',
			'terms'     => array( 'category-1' ), // Replace category-1 with the slug of your category
		),
	)
) );

Only show specific campaigns, by ID

$campaigns = Charitable_Campaigns::query( array(
	'post__in' => array( 111, 222, 333 )
) );