Last active
September 30, 2020 12:18
-
-
Save IronGhost63/f8069fa642dbb4ba94e5f41b779aa870 to your computer and use it in GitHub Desktop.
WP_Query only parent categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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