Raw SQL in Models

You can bundle all of the related queries in the same object. It will make it much easier to create a central area that contains all queries against a custom post type :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
namespace App\Model;

use WP_Query;

class Artist extends AppCustomPostType {

    public static function findPublished()
    {
        $config = array(
            'post_type'     => self::wordpressKey(),
            'posts_per_page' => -1,
            'post_status' => 'publish',
        );

        $data = new WP_Query($config);

        return $data->posts;
    }
}
?>

Every view and template files would then call the centralized App\Model\Artist::findPublished() ensuring the query is always correct, testable and unique. Note however, the internal Query class is a preferred method of handling WP_Query assignments as it is a more descriptive process.