How to pull through pages in wordpress according to template name

Hi all,

I’m trying to modify the behaviour of the homepage of a theme I’m using. At the moment the homepage pulls through all artist pages in the site - it’s doing this by featured image at the moment. So all artist pages have a featured image added to the page. This is the code in functions.php:

function my_get_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'page ') );
		$query->set( 'meta_key', '_thumbnail_id' );
		$query->set('orderby', 'rand');
		$query->set('posts_per_page', '100');
    }
}
add_filter( 'pre_get_posts', 'my_get_posts' );

Only now we want to be able to add featured images to other pages in the site for other reasons, but don’t want them to appear on the homepage. Each artist page is assigned to an ‘artist.php’ template, so I’m assuming it must be pretty straightforward to alter that function to only pull in pages with that template rather than featured image thumbnail.

After some searching on how other people have done something similar, I tried amending my code to the following:

function my_get_posts( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'page ') );
		$query->set( 'meta_key', '_wp_page_template' );
		$query->set( 'meta_value', 'artist.php' );
		$query->set('orderby', 'rand');
		$query->set('posts_per_page', '100');
    }
}
add_filter( 'pre_get_posts', 'my_get_posts' );

This breaks altogether and pulls nothing through. What am I missing here?!

Thanks in advance :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.