Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Last active December 21, 2015 02:28
Show Gist options
  • Save ryanmr/6235107 to your computer and use it in GitHub Desktop.
Save ryanmr/6235107 to your computer and use it in GitHub Desktop.
Include related episodes in the feed by getting the usual posts, reading their post meta value, and then re-running the query
<?php
// pre_get_posts is hooked to run `include_the_fringe`
public function include_the_fringe( $query ) {
if ( $query->is_feed() && $query->is_main_query() && isset($_GET['fringe']) ) {
$ids = $this->find_fringe_episodes($query);
// $ids = json_decode('["490","491","381","245","219","168","132","130","126","122"]');
$length = count($ids);
if ($length < $query->get('posts_per_rss')) $length = $query->get('posts_per_rss');
$query->set('posts_per_rss', $length);
$query->set('post__in', $ids);
$query->set('cat','');
// $query->set('category_name', '');
$query->set('tax_query', null);
}
return $query;
}
private function find_fringe_episodes($query) {
$qu = clone $query;
$qu->set('fields', 'ids');
$posts = $qu->get_posts();
$ids = array();
foreach ($posts as $post_id) {
$id = $post_id;
$fringe_id = get_post_meta($id, 'nexus-fringe-episode', true);
$ids[] = $id;
if (is_numeric($fringe_id)) $ids[] = $fringe_id;
}
wp_reset_query();
wp_reset_postdata();
return $ids;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment