WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude a custom post type from HTML sitemap (10 posts)

  1. maustin89
    Member
    Posted 1 year ago #

    Hi guys, I created a page template for an HTML sitemap based on Yoast's post here: http://yoast.com/html-sitemap-wordpress/

    The below snippet is used to display custom post types in the sitemap:

    <?php
    foreach( get_post_types( array('public' => true) ) as $post_type ) {
      if ( in_array( $post_type, array('post','page','attachment') ) )
        continue;
    
      $pt = get_post_type_object( $post_type );
    
      echo '<h2>'.$pt->labels->name.'</h2>';
      echo '<ul>';
    
      query_posts('post_type='.$post_type.'&posts_per_page=-1');
      while( have_posts() ) {
        the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      }
    
      echo '</ul>';
    }

    My problem is I'm using the WCK CPT Creator plugin and it apparently auto-generates a post type for custom meta boxes, because I'm getting a "WCK Custom Meta Boxes" section at the bottom of my sitemap below the other CPTs. I can't see any way to remove this in the plugin's interface. Can anyone suggest how to modify the above code to exclude a given post type?

    Thanks!

  2. bcworkz
    Member
    Posted 1 year ago #

    You have the right idea with the in_array() check, but the logic is reversed. You're saying to skip the rest if the post type is in the array. Use the '!' NOT operator in front of in_array() so the rest of the code is skipped for post types not in the array.

    if ( !in_array( $post_type, array('post','page','attachment') ) )
       continue;
  3. maustin89
    Member
    Posted 1 year ago #

    Hi bcworkz, thanks much for taking the time to reply.

    Modifying the code like you suggest prevents MY custom post type from displaying as well, which isn't what I'm after. I want to continue displaying my CPT as part of my sitemap, but exclude this other CPT that the plugin adds and is irrelevant to my sitemap.

    I learned that the post type is called "wck-meta-box", is there any way to modify the snippet above to basically add a check something like "if post type = wck-meta-box then skip it" ? I simply don't know enough about php to make that happen myself. Any help is much appreciated!

  4. bcworkz
    Member
    Posted 1 year ago #

    You could simply add your desired post type to the array in my suggested code, but it's probably more efficient to go back to your original script without the '!' NOT operator and remove all post types you want to display from the array and only include post types you do not want displayed. (I hope that makes sense.) Even if it's array of one post type to not display, I would leave it as an array structure so it's easy to add other post types if need be.

    There is a way to do what you suggest and skip display if it is only the wck-meta-box type and nothing else, but the array structure allows better flexibility for future modifications.

  5. maustin89
    Member
    Posted 1 year ago #

    Ohhhkay, I understand! BUT! When I added my custom post type "reviews" into the array, it worked as expected (reviews were not displayed, but WCK Meta Box still was.) When I put "wck-meta-box into the array though, the page seems to get stuck in an infinite loop. When I put a random nonexistent post type into the array like 'woot', I don't have this problem.

    I'm starting to think the solution is to not use this plugin. >>

  6. bcworkz
    Member
    Posted 1 year ago #

    :) I'm all for not using plugins unless absolutely necessary. The fewer the better. Most of my little tweaks are in one catchall plugin I created. Only when the task seemed too daunting have I resorted to a plugin.

    But there should be a simple solution to not displaying this particular post type. I think the loop is getting restarted by virtue of having the get_post_types() function within the foreach parameters. I can't explain why this only happens with single item arrays. Perhaps try assigning the function's return to a variable before running the loop:

    $types = get_post_types( array('public' => true) );
    foreach( $types as $post_type ) {

    Or simply abandon the array idea and eliminate the one post type with
    if('wck-meta-box' == $post_type) continue;
    as you suggested a couple posts back.

  7. maustin89
    Member
    Posted 1 year ago #

    So strange, both of those methods resulted in the same infinite loop outcome. I don't want to take up any more of your time trying to get to the bottom of this, at this point I will either find another plugin to use or bite the bullet and read up on how to manually add custom post types (which I probably should've done in the first place).

    Thank you nonetheless for all of your help, bcworkz! :)

  8. bcworkz
    Member
    Posted 1 year ago #

    You are most welcome. I appreciate your consideration for my time, but since I'm here, one last parting thought. I personally have no experience with continue structures, I only did a quick read in the PHP docs to confirm you were using it correctly. I may not be appreciating some nuance of this construct.

    I've always used simple if(){}else{} block structures, I feel it is easier to follow the code years later when I return to a project. The next thing I would do is stick with what I know and do something like

    if('wck-meta-box' != $post_type) {
       //echo out whatever for all other post types
    }

    If you still get infinite loops, something very queer is going on with the WP "loop" mechanism (have_posts() and what not). Good luck.

  9. bcworkz
    Member
    Posted 1 year ago #

    Wait a minute!! Just as I hit POST, it dawned on me. the_post() must NOT be in the conditional or after the continue. It is what increments the counter checked in have_posts(). I neglected to point out this little tidbit. Only the portion generating output via echo should be in the conditional or after the continue.

    If that is it, I'll give myself a dope slap and apologize for not being more clear.

  10. maustin89
    Member
    Posted 1 year ago #

    ooo! I managed to get the desired outcome by wrapping the lines that echo the h2 in your conditional. The final snippet is:

    <?php
    foreach( get_post_types( array('public' => true) ) as $post_type ) {
     if ( in_array( $post_type, array('post','page','attachment') ) )
        continue;
    
      $pt = get_post_type_object( $post_type );
    
     if('wck-meta-box' != $post_type) {
       echo '<h2>'.$pt->labels->name.'</h2>';
       echo '<ul>';
     }
    
      query_posts('post_type='.$post_type.'&posts_per_page=-1');
      while( have_posts() ) {
        the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      }
    
     if('wck-meta-box' != $post_type) {
       echo '</ul>';
     }
    }
    ?>

    I suppose it only works because this post type is empty, otherwise I'd need to do the same for the echo following the_post(). I know next to nothing about php, but repeating the same if statement three times strikes me as a touch inelegant. :D Nonetheless, this is exactly what I was hoping to achieve.

    Thank you once again, my sweet!

Topic Closed

This topic has been closed to new replies.

About this Topic