WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] problems with is_post_type() - perhaps it is a bug? (25 posts)

  1. pmdci
    Member
    Posted 3 years ago #

    Hi there,

    I added the following conditional in my sidebar.php template, before the check for widgets:

    <?php if ( is_single() && is_post_type('hotel') ) { include 'stats-hotel.php'; } ?>

    The is_single() conditional works fine. So it only includes the stats-hotel.php file when viewing single posts.

    HOWEVER the is_post_Type() function doesn't work properly. In my case, besides the default post type I also have hotel and resort as post types. Nonetheless the file stats-hotel.php is being included when viewing every post type I have (post, hotel, resort), when it should only be included when seeing the post type hotel.

    HOWEVER, if I change the is_post_type() argument and add some garble (i.e.: an unexistent post type), for example:

    is_post_type('garblegarblegarble')

    Then the file stats-hotel.php is never included when seeing any type of post type.

    Am I doing something wrong, or this is really a bug?

    Regards,
    P.

  2. MichaelH
    Member
    Posted 3 years ago #

    If you have a registered post type of "hotel" then this would always be true:

    is_post_type('hotel')

    You might want this instead:

    <?php
    $queried_post_type = get_query_var('post_type');
    if ( is_single() && 'hotel' ==  $queried_post_type ) {
      echo 'this single view is of a post type hotel';
    }
    ?>

    Note: thanks for fixing the Conditional Tags Codex article.

  3. pmdci
    Member
    Posted 3 years ago #

    MichaelH,

    This is a great feedback thank you.
    (and thanks for noticing my fix in the codex :-) )

    I think I will update the codex with the code you have provided with a 'Snipted ' with this code if I get it to work. Is that ok?

    But it didn't work for me, though. Here is what I did:

    <?php // get stats dashboard
    $queried_post_type = get_query_var('post_type');
      if ( is_single() && 'post' ==  $queried_post_type ) { include 'dashboard.php'; } ?>

    I know I said in my previous example that the post type I was trying to query was hotel. But in all honesty I was just trying to simplify things. Perhaps I confused things even more?

    If so I apologise. But I need this include to happen ONLY in single pages of type post (the defaul post type).

    Even big WP Gurus on the internet, as per what I read in their blogs, thought that this function is_post_type() would work as I thought...

  4. pmdci
    Member
    Posted 3 years ago #

    MichaelH,

    Please see my message above as I edited it. (I was counting victory before winning the battle...)

    I didn't get the result I expected... :(

    As a matter of fact I tried to add a test of <?php echo $queried_post_type; ?> and I got nothing in return. Perhaps I must declare some variable global somewhere? What do you think?

    I also tried adding the following in my single.php file:

    <?php if (have_posts()) : while (have_posts()) : the_post();
      $queried_post_type = get_query_var('post_type');
      $city_as_text = get_the_term_list( $post->ID, 'city', '', ', ', '' );
      echo $queried_post_type; ?>

    But it also didn't work...

  5. MichaelH
    Member
    Posted 3 years ago #

    Evidently the query var for post_type doesn't get populated on a post.

    Could be a bug.

    It does get set for the custom post types like your hotel.

    Maybe you might consider reporting that as a bug to trac.

  6. pmdci
    Member
    Posted 3 years ago #

    MichaelH,

    I would be more than happy to repot bugs. I went to track before but I couldn't see a link for creating an account...

    How can I do it?

    Regards,
    Pedro

  7. MichaelH
    Member
    Posted 3 years ago #

    Use your forum login there.

  8. pmdci
    Member
    Posted 3 years ago #

    Cool! It worked. I just raised the ticket here (#13535), making sure I pointed to this forum post.

    Thanks MichaelH. Hope you guys can make it work (that is, if it is a bug and not my fault, hehe)

    Regards,
    P.

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    I'm not able to reproduce your issue, using echo get_query_var('post_type') prints out the relevant post type for a custom post type.

    Have a look back at your register_post_type code and check you have the query_var parameter set to true (if it's set to false, then there's the source of your problem).

    NOTE: I've noticed when registering post types manually, i've had to re-save(just click the save button) the permalink settings in order for the necessary rewrite rules to get created and added to the rewrite array (which you'll need if using any permalinks, ie. non-default).

  10. MichaelH
    Member
    Posted 3 years ago #

    I'm not able to reproduce your issue, using echo get_query_var('post_type') prints out the relevant post type for a custom post type.

    But what if the post type is "post"?

  11. Mark / t31os
    Moderator
    Posted 3 years ago #

    It's empty when it's a post or page, but i'd expect that because they're builin post types which are already accounted for with their own set of rules.

  12. MichaelH
    Member
    Posted 3 years ago #

    Yep, that is the question at hand ;)

  13. Mark / t31os
    Moderator
    Posted 3 years ago #

    Ok, i see, so why not look at the $posts object?

    Off the top of my head..

    $post_type = get_post_type( $posts[0] );

    Or.

    $post_type = get_post_type( $post );

    Might need to global the necessary var in your sidebar, but it should be sufficient (i think).

  14. Od3n
    Member
    Posted 3 years ago #

    i got the same probs too. is_post_type('post') doesnt works correctly.

  15. Od3n
    Member
    Posted 3 years ago #

    use is_singular($post_type) seems doing well.

  16. Mark / t31os
    Moderator
    Posted 3 years ago #

    is_post_type is not a conditional statement like alot of the is_* functions, it's intended to be used to check if a post_type exists.. (poorly named function).

    is_post_type( 'post' );

    ..would be true for any install of WordPress..

    is_post_type( 'book' );

    ..would be true for any install where a post type of book has been registered..

    With a bit of luck hopefully it will be replaced by something more appropriately named.
    http://core.trac.wordpress.org/ticket/13747

  17. pmdci
    Member
    Posted 3 years ago #

    @od3n:

    I just tried your idea of using is_singular($post_type) and it didn't work. Here is my sidebar.php sample:

    <?php // stats da anunciante
      if ( is_single() &&  is_singular('post') ) { include 'dashboard.php'; } ?>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
    [...]

    Basicaly I am getting the same behaviour with is_singular($post_type) as if using is_post_type(), as described by our chap t310s_. That is, it returns true as long as $post_type is a registered post type in my WP system.

    Would you be kind enough to share your example with us, so we can see how you made it work? According to the is_singular article in the codex, it doesn't have any example of this function supporting arguments (i.e.: post types, as in your example of is_singular('post')). If it does accept then the codex should be updated, and you would be doing the WP community a great favour and helping us with the documentation =)

    In the meantime, I will now try t31os_'s suggestion of using $post_type = get_post_type( $post ); and see where I can go with it... =(

  18. pmdci
    Member
    Posted 3 years ago #

    I solved the problem with t31os_'s suggestion. Thanks a lot dude!

    <?php
      $post_type = get_post_type( $post );
      if ( is_single() &&  $post_type == 'post' ) { include 'dashboard.php'; } ?>
      <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
  19. Mark / t31os
    Moderator
    Posted 3 years ago #

    Good stuff, happy to help.. :)

  20. Od3n
    Member
    Posted 3 years ago #

    @pmdci

    here the code i use.

    <?php if(is_singular('product')) { ?>
    		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(Products) ) : ?>
    		<?php endif; ?>
    	<?php } else { ?>
    		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
    		<?php endif; ?>
    	<?php } ?>

    before this i use is_single() && is_post_type($post_type) but they doesnt seems to work.

  21. Leroy12
    Member
    Posted 3 years ago #

    I confirmed that is_singular('post_type') is working for fine! Indeed, the "is_post_type" function is poorly named...

  22. pmdci
    Member
    Posted 3 years ago #

    @leroy12

    I didn't managed to make is_singular('post_type') to work. But is_single() && $post_type == 'post' worked.

  23. Fleetstar
    Member
    Posted 3 years ago #

    For is_singular to work with post_type you need to write it like this:

    is_singular( $post_type = your_post_type )

    This works for me.

  24. chadvonlind
    Member
    Posted 3 years ago #

    Strangely, I couldn't get this to work in my functions.php file.

    if(is_singular('missions')) {
         wp_enqueue_script('missionscript', get_bloginfo('template_url') . '/assets/js/mission.js', array('jquery'), '1', true);
    }

    So I ended up having to just call it in my footer.

    <?php if(is_singular('missions')) { // Only gets called on Mission pages ?>
     <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/assets/js/mission.js"></script>
    <?php  } ?>

    Any idea why this wouldn't work with the functions file?

  25. Clicknathan
    Member
    Posted 3 years ago #

    I use if ( 'post_type' == get_post_type())

    Where post_type is the name of your custom post type. Works like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic