Query Post Based of Yoast Primary Category Class

Yoast SEO stores the primary term ID as the post meta field _yoast_wpseo_primary_{taxonomy}. Knowing this you can use a meta query to get all posts with primary category ID X:

$query = new WP_Query([
    'cat' => 'X', // Just in case Yoast data corrupted & post no longer attached to X term but primary meta remains
    'meta_query' => [
        [
            'key' => '_yoast_wpseo_primary_category',
            'value' => 'X',
        ]
    ],
]);