Filtrer les articles WordPress mis en avant

To filter featured WordPress posts, you can use the "pre_get_posts" action hook along with a custom function. First, create a new function to modify the main query using "pre_get_posts". Within this function, use the "is_home" conditional tag to check if you are on the homepage, and the "is_main_query" method to ensure it's the main query. Then, set the "meta_query" parameter to filter posts based on the presence of the "featured" meta key. Here's an example:

function filter_featured_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'meta_query', array(
            array(
                'key' => 'featured',
                'value' => '1',
                'compare' => '='
            )
        ) );
    }
}
add_action( 'pre_get_posts', 'filter_featured_posts' );

This code will filter the featured posts on the WordPress homepage based on the existence of a "featured" meta key with a value of "1".