1

If I use pre_get_posts this way

function ef_company_query($query) {
  if ( !is_admin() && $query->is_main_query() && $query->is_post_type_archive( 'company' ) ) {
    $query->set('post_type', array( 'company', 'mission', 'values', 'staff' ) );
  }
}

add_action('pre_get_posts','ef_company_query');

it seems that wordpress won't load archive-company.php but archive.php.

How should I use pre_get_posts to append extra CPTs to an archive page?

EDIT 1

I mentioned archive-company.php because I want to load the extra CPTs at URL www.website.com/company.

EDIT 2

I'm open to any solution which pulls a specific template based on the URL www.website.com/company and allows the addition of the extra CPTs (preferably via pre_get_posts, performance wise).

EDIT 3

Trying to make things more clear here!

I want to be able to load several CPTs on the URL www.website.com/company.

  1. Which template should/can I use?
  2. How to append the CPTs to the main query (hopefully via pre_get_posts, performance wise)?

I'm trying to avoid the use of new WP_Query() and having multiple loops in the page.

1 Answer 1

1

You seems to assume that it would grab archive for first of multiple post types. But why? Order in this case does not indicate any kind of priority.

WP seems to consider that logical as well. If you take a look at get_archive_template() the individual template will only be considered if there is only one post type in query.

Multiple post types in query will cause archive.php to load, as you are observing.

If you want to load more specific template, you would need to override that via filters as well, for example with archive_template one (dynamic one in get_query_template()).

6
  • I wasn't assuming that actually! The reason why I mentioned archive-company.php is because I want to load those extra CPTs at www.website.com/company/. Aug 15, 2016 at 21:18
  • Well, then it's not clear what template has to do with your question. URL and template are not directly connected. Template depends on query which depends on URL.
    – Rarst
    Aug 15, 2016 at 21:44
  • I've already updated my question. I would be glad for any solution which pulls a template dependent on the URL www.website.com/company/ and allows the query to add the extra CPTs. Aug 15, 2016 at 22:40
  • Adding CPTs to query you already seem to have. Customizing template of result is covered in my answer. You might want to try to rephrase your question as what you expect result of your code to be exactly and what actually happens.
    – Rarst
    Aug 15, 2016 at 22:50
  • I've looked into get_query_template but couldn't find any hint on how they could help me with my issue. As far as I could understand get_query_template will only retrieve the path of the archive template… Aug 15, 2016 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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