• I’m using custom post types in WordPress and I’m also looking for ways to improve the way they work.

    I have created an archive-retailer.php for my retailer custom post type, and I’ve also added a * Template Name: Retailers to the file so I can use it as a page template. (the file has conditional code for both the archive and the page)

    The slug for my retailer custom post type is ‘retailers’, so the archive of this custom post is /retailers. When I’m using the archive-retailer.php page template on a page with the slug ‘retailers’, whant visiting the /retailers link the code for the archive template is used instead of the code for the page.

    Is there a way to overwrite this behaviour? Should I start a discussion and see if this behaviour (allow page slugs to overwrite the archive slugs) could be added to the core?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    If the page slug is ‘yoursite.com/retailers’ and the cpt archive slug is ‘yoursite.com/retailers’ how should WordPress know which one you want to show?. Change the rewrite parameter when registering the custom post type or change the slug for the page. And why do you want to use the same template for a custom page and a cpt archive?

    Thread Starter Nicolae Pop

    (@nyxko)

    That’s actually what I’m proposing, why not show the page instead of the post archive when accessing /retailers

    I’m using the same template because the loop showing the custom post type is the same for both of the page template and the archive template. When adding a page using the ‘Retailers’ page template, I can allow the editors to use a custom name for the listings (the page’s title), and also add an intro text (via the page content). This page will also appear in searches, unlike the custom post archive page.

    Plus, I’m trying to use the get_post_type_archive_link to show the visitors a link back to the custom post type archive. That’s why I want the page to share the same slug as the archive index.

    Moderator keesiemeijer

    (@keesiemeijer)

    So at yoursite.com/retailers’ you always want to use the page template. Why the need for the cpt archive at the same url? Or why the need for a cpt archive at all ( register with ‘has_archive’ => false, )?

    That’s actually what I’m proposing, why not show the page instead of the post archive when accessing /retailers

    And what should people do when they would rather show the cpt archive?

    Change one of the two slugs if you want them both.

    Or try it by hooking into the template_include filter (in functions.php).

    add_filter( 'template_include', 'include_page_template', 99 );
    
    function include_page_template( $template ) {
      // WordPress thinks it's on a cpt archive
      if ( is_post_type_archive( 'retailer' ) )
        $template = locate_template( array( 'archive-retailer.php' ) ); // use page template
    
      return $template;
    }

    You can set which post types you want to include in search results with the pre_get_posts action (in functions.php):

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        // set post types for search results
        if(is_search()){
          $query->set('post_type', array('post','page','retailer'));
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page slug priority over post archive slug’ is closed to new replies.