Skip to content

Instantly share code, notes, and snippets.

@ericnicolaas
Last active April 13, 2018 02:26
Show Gist options
  • Save ericnicolaas/9f9ce566236ccc67919df9f1472503c8 to your computer and use it in GitHub Desktop.
Save ericnicolaas/9f9ce566236ccc67919df9f1472503c8 to your computer and use it in GitHub Desktop.
Examples of campaign queries using Charitable_Campaigns
<?php
// Default usage.
//
// This will get the most recent X campaigns, where X is the
// same as the number of posts you have set to show on a blog
// page (defaults to 10).
$campaigns = Charitable_Campaigns::query();
// Only get 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 get 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 get campaigns in a certain category.
//
// This will get campaigns in the category-1 category.
$campaigns = Charitable_Campaigns::query( array(
'tax_query' => array(
array(
'taxonomy' => 'campaign_category',
'field' => 'slug',
'terms' => array( 'category-1' ),
),
)
) );
// Only get certain campaigns, by ID.
//
// This will get three campaigns, with IDs of 111, 222 and 333.
$campaigns = Charitable_Campaigns::query( array(
'post__in' => array( 111, 222, 333 )
) );
// Show all campaigns, but exclude one or more.
//
// This will exclude two campaigns, with IDs of 111 and 333.
$campaigns = Charitable_Campaigns::query( array(
'post__not_in' => array( 111, 333 )
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment