-1

I have a question about reasons why the main query would call the current page's info when using a template file, instead of querying the posts.

If I modify the loop by creating a custom query using new WP_query, I can access the all the posts on my custom template as I want. However, when I try use the main loop/query on my custom template, with plain-old have posts()and the_post() my page seems to querying info about the current page (e.g. when the_title and the_date match that of the current page, not my posts).

What is weird that this doesn't seem to be the case for my front-page. The main query for the front page is fine. Any ideas about why the main functions for the loop like "have posts" would call current the page for the template's info instead of querying the posts? What have I done wrong?

I used wp_reset_query() after the all loops when i use the main loop, and wp_reset_postdata() after all loops where I used new WP_query.

UPDATE:

Thanks to @Milo's answer, I have figured out that I misunderstood how WordPress works. I wanted to have custom templates for the achive page for a certain category, e.g. the category "special".

I was using:

  1. a custom template for the category, with an arbitrary name (e.g. special.php)
  2. and a category-archive reroute in archives.php, which pointed the category to my file , e.g. special.php

I was trying to use the main post query with those custom pages/templates, with arbitrary names.

I now realize that I can accomplish custom category pages that use a post-based main query more elegantly without the rerouting/archive.php solution. The better way is to rename the custom category .php file that I created, that had an arbitrary name ( e.g. special.php) with a name that matches WordPress' native category convention. This convention requires you to use a category- prefix in conjunction with the target category-name (e.g category-special.php).

I renamed that custom .php file with the a name using this category- convention, and deleted the rerouting I had created in archives.php. After taking those two steps, I was able grab posts of those categories only using the main loop functions (e.g. have_post() and the_post) AND have custom design/elements/html/php for that category.

Thanks @Milo!

There are a lot of intricacies to discover in WordPress but its nice to figure them out!

0

check for slug or url of the page and post. If there is page with the same name then it will query about the page.

  • I have three things that match, the page title, the page template, and a category, could this cause issues? The main query I'm testing is unmodified. When you say "if there is a page with the same name" what do you mean, the same name as what? – CoderScissorhands Jan 28 '17 at 18:19
  • I mean to check the slug... If the url is for page then it will have ` $post ` data of page. Why should it show posts? – Vishal Kumar Sahu Jan 28 '17 at 18:30
  • Or you may assign it in the general setting section... then it will query for the posts. – Vishal Kumar Sahu Jan 28 '17 at 18:31
  • check the slug for the category, the page, or the template? – CoderScissorhands Jan 28 '17 at 18:31
  • sorry can you use less pronouns....i need to know what you are referring to. – CoderScissorhands Jan 28 '17 at 18:31
0

The Main Query runs before the template is loaded. The results of the Main Query is how WordPress knows what template to load. Whatever queries you run in the template are unrelated to the Main Query.

The page you designate as the page for posts is a special case. When the request is parsed and it's determined that the request is for the page that displays the posts, the Main Query is populated with posts instead of that page's data. This is the only page that behaves this way.

Any other case, where you are creating custom queries in the template, the Main Query was already populated with that page's data before the template was loaded.

  • Hi, I think you might have answered my question, but I'm a noob and don't' quite understand what you said especially in the second paragraph. Are you saying that only the index/home/front-page/blog that is designated in my reading settings (static if designated, home otherwise), is the one that accesses the main post query? the first answer in this question seems relevant: wordpress.stackexchange.com/questions/64827/… – CoderScissorhands Jan 28 '17 at 19:12
  • Are you saying it is only possible to access the main post query on one page/template? – CoderScissorhands Jan 28 '17 at 19:12
  • 1
    Yes, by default you can have only a single instance of the main posts page, which is set in the Settings > Reading admin page. Is there a reason why you need another posts page? – Milo Jan 28 '17 at 19:18
  • I have custom category pages I built, I was trying to use pre_get_posts for them, to load them faster, but obviously before pre_get_posts can work, you need to get the main query working. I can use new WP_query but it is less elegant. Also, I wanted to modify my different queries pages for different pages all in one place (in functions.php). It seemed like a good way to go. – CoderScissorhands Jan 28 '17 at 19:21
  • But I assumed (I suppose, incorrectly so) that the main query ran on all pages, not just the main pages, that's why it seemed inelegant to use new WP_query(). If the main query doesn't run on all templates by default, then I guess I'm not making two queries, with one being wasted in this case..I suppose my new WP_query() would be the only query being called on my custom templates, so using it would not be wasteful. I did not understand the the main query didn't run on all templates/pages by default. – CoderScissorhands Jan 28 '17 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.