I basically have the same problem like in this question, but the correct answer doesn’t work for me. Yes, I get a list of posts from my CPT, but the archive-{post-type}.php is not used to render the page. It still uses the index.php. Any help would be great!

This is my code:

add_action('pre_get_posts', 'my_front_page');
function my_front_page($wp_query) {
  $post_type="foobar";

  if ( $wp_query->get('page_id') == get_option('page_on_front') ) {
    $wp_query->set('post_type', $post_type);
    $wp_query->set('page_id', '');

    // Fix conditional functions like is_front_page or is_single
    $wp_query->is_page = 0;
    $wp_query->is_singular = 0;
  }
}

2 Answers
2

Why don’t you try using a home.php file and place your desired loop/query within that file or alternatively use get_template_part to retrieve a file that contains your loop.

Can you please show us your code?

What I frequently do is structure my index.php like so,

<?php get_header(); ?>

    <?php if ( is_home() ) {

            get_template_part('home.php');

        } else {

           //run standard/default loop here

        } 
    ?>

<?php get_footer(); ?>

Then within home.php you can do literally whatever you like,

$args = array( 'posts_per_page' => 10, 'post_type' => 'custom_post_type' );
$wp_query = new WP_Query( $args ); 
while ( $wp_query->have_posts() ) : $wp_query->the_post();
 //get your title, content, tags, etc
endwhile;

With this approach you are NOT required to set a static front page within your dashboard settings under the normal Settings -> Reading -> Front page displays section.

Note: This is not the only way to achieve what you want, but a reliable method without having to resort to using any filters, if you’re not comfortable with using them or in the event you’re having trouble (of course saying this without seeing your code so far).

Leave a Reply

Your email address will not be published. Required fields are marked *