0

I am trying to display a different template depending on the post type.

I have registered two custom post types called resource and contact. When I try to get the post type in search.php (using get_post_type()), I always get back resource post type, even when the post is definitely contact.

Registering resource

function register_resource_post_type() {

    $args = array(
        'labels' => array('name' => __('Resources'), 'singular_name' => __('Resource'), 'add_new_item' => __('Add New Resource')),
        'public' => true,
        'supports' => array( 'title', 'custom-fields' ),
        'show_in_rest' => true,
        'rest_base' => 'resource-api'
    );

    register_post_type('resource', $args);

    function custom_enter_resource_title( $input ) {
        if ( 'resource' === get_post_type() ) {
            return __( 'Enter resource title' );
        }

        return $input;
    }
    add_filter( 'enter_title_here', 'custom_enter_resource_title' );
}
add_action('init', 'register_resource_post_type');

Registering contact

function register_contact_post_type() {

    $args = array(
        'labels' => array('name' => __('Contacts'), 'singular_name' => __('Contact'), 'add_new_item' => __('Add New Contact')),
        'public' => true, 
        'supports' => array( 'title', 'custom-fields' ),
        'show_in_rest' => true,
        'rest_base' => 'contact-api'
    );

    register_post_type('contact', $args);

    function custom_enter_contact_title( $input ) {
        if ( 'contact' === get_post_type() ) {
            return __( 'Enter contact title' );
        }

        return $input;
    }
    add_filter( 'enter_title_here', 'custom_enter_contact_title' );
}
add_action('init', 'register_contact_post_type');

Search.php

        while ( have_posts() ) :
            the_post();

            $post_type = get_post_type() <---- THIS IS ALWAYS resource
        endwhile;
  • Have you tried get_post_type( get_the_ID() ); ? – MikeNGarrett Mar 18 '19 at 13:29
  • Yep. I get the same post_type. – Ged Mar 18 '19 at 13:50
  • 1
    I ran everything you have and it's all working as expected. I'm sure you've already done this, but I would go back and make sure your content is published and your search term matches the title you're looking for correctly. If that all looks right, go back to the raw query and see if you can run the raw mysql query from $wp_query on the database and get a different result. – MikeNGarrett Mar 18 '19 at 13:59
  • get_post_type() is not a complicated function. Unless you're deliberately filtering it somewhere else, if it says the post type is resource, it is. Mike is right, you need to make sure you're publishing as the correct post type. – Jacob Peattie Mar 18 '19 at 14:32
  • 1
    Can you share more about your while loop? If you're using $post_type outside of (after) the loop, then it will always be the post type of the very last post, because it's overwriting itself for each post. If you're using it inside the loop, do you have other queries running on this page and maybe need to do a wp_reset_query() before this loop? – WebElaine Mar 18 '19 at 14:49
0

The comment

get_post_type() is not a complicated function

led me to disable the Relevanssi plugin that I was using for search and determined that "Create custom search result snippets" setting had been selected and was causing issues with the post type being returned. Once I unchecked this, everything worked.

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.