Skip to content

Instantly share code, notes, and snippets.

@IronGhost63
Last active September 30, 2020 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IronGhost63/f8069fa642dbb4ba94e5f41b779aa870 to your computer and use it in GitHub Desktop.
Save IronGhost63/f8069fa642dbb4ba94e5f41b779aa870 to your computer and use it in GitHub Desktop.
WP_Query only parent categories
<?php
namespace App;
use \WP_Query;
$categories = get_categories( ['parent' => 0] );
$ids = array_map( function( $category ) {
return $category->term_id;
}, $categories);
// Or with arrow function as in PHP 7.4
// $ids = array_map( fn($category) => $category->term_id, $categories );
$posts = new WP_Query( [
'category__in' => $ids,
] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment