8

In my website I have three custom post types: scripts, scenes and plugins. When visiting the archive page of a single post type (i.e. by going to mysite.com/plugins) you correctly see all the posts of that type.

In archive.php, how can I find out which custom post type the user is looking at right now?

I tried the following:

<?php 
    global $post; 
    $postTypeLabels = get_post_type_labels(get_post_type_object($post)); 
    echo var_export($postTypeLabels); 
?>

But I'm getting the this:

Post name is stdClass::__set_state(
  array( 'name' => 'Posts', 
    'singular_name' => 'Post', 
    'add_new' => 'Add New', 
    'add_new_item' => 'Add New Post', 
    'edit_item' => 'Edit Post', 
    'new_item' => 'New Post', 
    'view_item' => 'View Post', 
    'search_items' => 'Search Posts', 
    'not_found' => 'No posts found.', 
    'not_found_in_trash' => 'No posts found in Trash.', 
    'parent_item_colon' => NULL, 
    'all_items' => 'All Posts', 
    'menu_name' => 'Posts', 
    'name_admin_bar' => NULL, 
  )
)

I'm guessing that, since I am in an archive page, the $post is not correct.

P.S. I know that I can create archive-plugins.php for the plugins archive. Unfortunately, I have installed a theme that, as far as I know, kinda prevents that. So this is not an option.

6 Answers 6

15

There are a several of ways to do this. Put:

var_dump($wp_query->query,get_queried_object()); die;

In your archive.php and you should see two of those ways.

$wp_query->query will have post_type component for custom post types. That will not be there for post post types. get_queried_object will return quite a bit of data for custom post types but null for post post type.

There are also some related template tags that might help. is_post_type_archive comes to mind.

Between those you should have the information you need to put together whatever logic you need. It is not clear from you question what the end result is supposed to be, so I can't really write much.

Since you specifically named archive.php that is what I tested in. You may need different code for some other template, especially with get_queried_object which returns very different information depending on the context.

4
  • get_query_object() was what I was looking for. I was basically looking for how to get the name of the custom post type, but I'd rather keep the question more open for anybody else. Thanks.
    – pek
    Commented Mar 17, 2013 at 23:18
  • 1
    Basically, this is what I did to get the name of the custom post type in the archive.php: $qObj = get_queried_object(); echo $qObj->labels->name;
    – pek
    Commented Mar 17, 2013 at 23:19
  • 3
    For anyone passing by: be wary of using get_queried_object(). If is_post_type_archive() is true but you're also using taxonomy-based filtering, the taxonomy term will take precedence. In this case get_queried_object() will return a taxonomy term object instead of a post type object, even though is_post_type_archive() is true.
    – Mark
    Commented Sep 3, 2015 at 15:14
  • get_queried_object() will return a number of different objects depending on context.
    – s_ha_dum
    Commented Sep 3, 2015 at 15:26
10

Here is the function you want:

/**
 * Get the current archive post type name (e.g: post, page, product).
 *
 * @return String|Boolean  The archive post type name or false if not in an archive page.
 */
function get_archive_post_type() {
    return is_archive() ? get_queried_object()->name : false;
}
6

I see this in a few comments on the accepted answer, but wanted to make this point clear for others skimming over these answers: The global $wp_query object is more reliable for getting the archive's post type. Specifically from $wp_query->query['post_type'].

You can use get_queried_object() but it has caveats. Namely if you have other query parameters like taxonomy terms. In that case get_queried_object() will return a WP_Term object instead of the post type you are probably looking for.

So if the archive has a clean query for a post type, then get_queried_object() will work. But for more reliability use the global $wp_query object.

Here is a function you can use in your theme for this:

/*
* PURPOSE : If there are zero results (or other parameters) in the archive query, get_post_type() isn't reliable for knowing what the archive's post type is. This function gets the post type from the global $wp_query object instead.
*  PARAMS : n/a
* RETURNS : boolean / string - the slug for the post type fromm $wp_query, or false if that is not found.
*   NOTES :
*/
function jp_get_archive_post_type(){
    $post_type = false;

    global $wp_query;
    if( isset($wp_query->query['post_type']) ){
        $post_type = $wp_query->query['post_type'];
    }

    return $post_type;
}
2
die(var_dump(get_taxonomy(get_queried_object()->taxonomy)->object_type));

I thinks that is the answer for your question.

Happy coding!!!

1
  • It returns NULL, even i tried it on an archive page. Not good. Commented Dec 7, 2016 at 10:17
2

Why not just use get_query_var('post_type')?

https://developer.wordpress.org/reference/functions/get_query_var/

3
  • Try to add a bit more info in your answers, this is a comment at best. Commented Feb 10, 2021 at 19:26
  • 2
    Well there are no other info. The question is: In archive.php, how can I find out which custom post type the user is looking at right now? With get_query_var('post_type') you get which custom post type the user is looking at right now Commented Feb 11, 2021 at 3:05
  • TBH this should be the accepted answer
    – MikeeeG
    Commented Oct 11 at 11:07
0

No other nice option rather than template making!

Or Including a template file when that custom post type archive (according to its url) is accessed.

See further links for more info:

http://codex.wordpress.org/Function_Reference/load_template

wordpress - load a template based on the URI

http://www.mihaivalentin.com/wordpress-tutorial-load-the-template-you-want-with-template_redirect/

Your Answer

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

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