Skip to content

Commit

Permalink
refactor: Use event_query to clean up post retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Mar 3, 2019
1 parent ff61b30 commit c145193
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 236 deletions.
48 changes: 10 additions & 38 deletions archive-colloquium.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,18 @@
);

// Get upcoming colloquia
$colloquia = get_posts(
array(
'numberposts' => -1,
'post_type' => 'colloquium',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $seasonstart,
'compare' => '>='
)
)
)
);
$colloquia = event_query(array(
'post_type' => 'colloquium',
'order' => 'ASC',
'after' => $seasonstart
));

// Get upcoming miscellaneous events
$miscevents = get_posts(
array(
'numberposts' => -1,
'post_type' => 'miscevent',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dtstart',
'value' => $seasonstart,
'compare' => '>='
),
array(
'key' => 'dtend',
'value' => $seasonstart,
'compare' => '>='
)
)
)
);
$miscevents = event_query(array(
'post_type' => 'miscevent',
'order' => 'ASC',
'after' => $seasonstart
));

// Display events header and navigation
?>
Expand Down
114 changes: 33 additions & 81 deletions archive-concert.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,20 @@
);

// Get archived colloquia
$colloquia = get_posts(
array(
'numberposts' => -1,
'post_type' => 'colloquium',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $season,
'compare' => 'BETWEEN'
)
)
)
);
$colloquia = event_query(array(
'post_type' => 'colloquium',
'order' => 'ASC',
'after' => $seasonstart,
'before' => $seasonend,
));

// Get archived miscellaneous events
$miscevents = get_posts(
array(
'numberposts' => -1,
'post_type' => 'miscevent',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $season,
'compare' => 'BETWEEN'
)
)
)
);
$miscevents = event_query(array(
'post_type' => 'miscevent',
'order' => 'ASC',
'after' => $seasonstart,
'before' => $seasonend,
));

//
// Let’s start building a select menu
Expand All @@ -101,61 +81,33 @@
// Set query variables for each year
$querystart = $year . '0901';
$queryend = ($year + 1) . '0831';
$query = array($querystart,$queryend);

// Get archived concerts
$concertcheck = get_posts(
array(
'numberposts' => 1,
'post_type' => 'concert',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $query,
'compare' => 'BETWEEN'
)
)
)
);
$concertcheck = event_query(array(
'maxposts' => 1,
'post_type' => 'concert',
'order' => 'ASC',
'after' => $querystart,
'before' => $queryend,
));

// Get archived colloquia
$colloquiumcheck = get_posts(
array(
'numberposts' => 1,
'post_type' => 'colloquium',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $query,
'compare' => 'BETWEEN'
)
)
)
);
$colloquiumcheck = event_query(array(
'maxposts' => 1,
'post_type' => 'colloquium',
'order' => 'ASC',
'after' => $querystart,
'before' => $queryend,
));

// Get archived miscellaneous events
$misceventscheck = get_posts(
array(
'numberposts' => 1,
'post_type' => 'miscevent',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $query,
'compare' => 'BETWEEN'
)
)
)
);
$misceventscheck = event_query(array(
'maxposts' => 1,
'post_type' => 'miscevent',
'order' => 'ASC',
'after' => $querystart,
'before' => $queryend,
));

if ($concertcheck || $colloquiumcheck || $misceventscheck) {
$menuitems = array_merge($menuitems, array($year));
Expand Down
77 changes: 22 additions & 55 deletions front-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,30 @@
<?php endif;

// Get upcoming concerts
$today = date('Ymd', strtotime('-1 day'));
$concerts = get_posts(
array(
'numberposts' => 1,
'post_type' => 'concert',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $today,
'compare' => '>'
)
)
)
);
$today = date('Ymd', strtotime('now'));

$concerts = event_query(array(
'maxposts' => 1,
'post_type' => 'concert',
'order' => 'ASC',
'after' => $today,
));

// Get upcoming colloquia
$colloquia = get_posts(
array(
'numberposts' => 3,
'post_type' => 'colloquium',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'dtstart',
'value' => $today,
'compare' => '>'
)
)
)
);
$colloquia = event_query(array(
'maxposts' => 3,
'post_type' => 'colloquium',
'order' => 'ASC',
'after' => $today,
));

// Get upcoming miscellaneous events
$miscevents = get_posts(
array(
'numberposts' => 1,
'post_type' => 'miscevent',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'dtstart',
'value' => $today,
'compare' => '>'
),
array(
'key' => 'dtend',
'value' => $today,
'compare' => '>'
)
)
)
);
$miscevents = event_query(array(
'maxposts' => 1,
'post_type' => 'miscevent',
'order' => 'ASC',
'after' => $today,
));

// Display upcoming events
if ($concerts || $colloquia || $miscevents) : ?>
Expand Down
20 changes: 2 additions & 18 deletions page-music.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,10 @@
<?= component('edit_button') ?>
<section>
<?php
// custom filter to replace '=' with 'LIKE'
function my_posts_where($where)
{
return str_replace("meta_key = 'programme_$", "meta_key LIKE 'programme_%", $where);
}
add_filter('posts_where', 'my_posts_where');

// Get concerts with valid embed links in their programmes
$concerts = get_posts(array(
'suppress_filters' => false,
'numberposts' => -1,
$concerts = event_query(array(
'post_type' => 'concert',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'DSC',
'meta_query' => array(array(
'key' => 'programme_$_embed_link',
'value' => 'http',
'compare' => 'LIKE'
))
'has_av' => true
));

// access global embed variable for later
Expand Down
54 changes: 10 additions & 44 deletions single-member.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,18 @@
$testID = get_the_ID();

// Get archived colloquia
$colloquia = get_posts(
array(
'numberposts' => -1,
'post_type' => 'colloquium',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'fname',
'value' => $testID,
),
array(
'key' => 'colloquium_type',
'value' => 'HGNM Member',
)
)
)
);

// custom filter to replace '=' with 'LIKE'
function my_posts_where($where)
{
$where = str_replace("meta_key = 'programme_$", "meta_key LIKE 'programme_%", $where);
return $where;
}

add_filter('posts_where', 'my_posts_where');
$colloquia = event_query(array(
'order' => 'ASC',
'post_type' => 'colloquium',
'ft_composer' => $testID,
));

// Get archived concerts
$concerts = get_posts(
array(
'suppress_filters' => false,
'numberposts' => -1,
'post_type' => 'concert',
'meta_key' => 'dtstart',
'orderby' => 'dtstart',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'programme_$_composer',
'value' => $testID,
)
)
)
);
$concerts = event_query(array(
'order' => 'ASC',
'post_type' => 'concert',
'ft_composer' => $testID
));

$upcomingcolloquia = $colloquia;
$pastcolloquia = $colloquia;
Expand Down

0 comments on commit c145193

Please sign in to comment.