Something is really tripping me up. I have been used to querying posts via custom queries however based on research lately I have learned that for the most part this is a big no no. So, in testing the standard wp loop, I realized it does not work on for example, page-mypage.php. Can anyone share insight on this?
If I do something like get_title, it just gives the title for that page. I realize there is other posts about this topic on stack but all the up-voted responses are to use a custom query which again, as I have understood, is a poor method and pre_get_posts
should be used instead. However, I tried using pre_get_posts
function and that did not work either.
add_action( 'pre_get_posts', 'display_posts' );
function display_posts( $query ) {
if ( ! is_admin() && $query->is_main_query()) {
} // end if;
}
Does the standard loop only work on index.php? So I am assuming the global query does not run before page.php is called? Im totally confused here as well because how would wp know to display posts on, for example, page-mypage.php unless you are querying them directly in the template?
Again, I am using the standard loop, and it is not displaying any post data.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>