Skip to content

Instantly share code, notes, and snippets.

@justiny
Last active December 7, 2018 16:29
Show Gist options
  • Save justiny/4e416f99563e99dfc2f959508425823e to your computer and use it in GitHub Desktop.
Save justiny/4e416f99563e99dfc2f959508425823e to your computer and use it in GitHub Desktop.
Get latest posts from category, excluding current post
<?php
// convert to timber
$current_post_id = get_the_ID();
$current_post_cats = get_the_category();
$current_post_first_cat_id = $current_post_cats[ 0 ]->term_id;
$args = array(
'cat' => $current_post_first_cat_id,
'post__not_in' => array( $current_post_id )
);
$context['related-posts'] = Timber::get_posts($query);
<?php
// Get the current post id.
$current_post_id = get_the_ID();
// Get the current post's category (first one if there's more than one).
$current_post_cats = get_the_category();
$current_post_first_cat_id = $current_post_cats[ 0 ]->term_id;
// Setup arguments.
$args = array(
// Get category's posts.
'cat' => $current_post_first_cat_id,
// Exclude current post.
'post__not_in' => array( $current_post_id )
);
// Instantiate new query instance.
$my_query = new WP_Query( $args );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment