WordPress get_posts by category

In all probability you are using a custom taxonomy, and not the build-in category taxonomy. If this is the case, then the category parameters won’t work. You will need a tax_query to query posts from a specific term. (Remember, get_posts uses WP_Query, so you can pass any parameter from WP_Query to get_posts)

$args = [
    'post_type' => 'product',
    'tax_query' => [
        [
            'taxonomy' => 'my_custom_taxonomy',
            'terms' => 7,
            'include_children' => false // Remove if you need posts from term 7 child terms
        ],
    ],
    // Rest of your arguments
];

ADDITIONAL RESOURCES

Leave a Comment