WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post type issue >.> (7 posts)

  1. Adam
    Member
    Posted 1 year ago #

    So I made my own custom post type and after registering it I added:

    global $wp_rewrite;
    	  $wp_rewrite->flush_rules();

    to flush the rewrite rules. Everything was fine till I tried to create a post in that custom post type, when I did and went to view the site I got one of two things that went horribly wrong:

    One - The page, no matter what category I went to, what page I went to, what - what ever - I went to always displayed THAT post from the custom post type.

    Two - Deleting that post causes me to see a 404 error....

    I cant show you the site cause I am working on it locally. Any ideas?

  2. marv51
    Member
    Posted 1 year ago #

    Just to make sure, you shouldn't call flush_rules() on a normal page load, are you doing that?
    During development, you can flush the rules manually by going to the permalink page in the dashboard and hitting save.

    You could try to comment out the cpt registration, flsh and see if your site is working again.
    If only the registration is the part that is causing the problem, maybe you could post that call here. (Careful more than 10 lines should go in a pastbin.com, forum rules)

  3. Adam
    Member
    Posted 1 year ago #

    Not sure what you mean by cpt registration or flsh

    But this is my code, the top part is the custom post types while the bottom part is me walking through one called slides, which after further debugging found out that out of my three post types I have here - Only slides is causing this issue, if I remove the code it does nothing.

    With slides, if I make a slide, then it always shows that slide, no matter what. If I delete that slide I geta 404 error.

    See the code: here

    I am sure that the other post types will cause the same issue, but if I make a post using the slides custom post type, The essential layout of my page is:

    Header - Nav, Slider with that post in it (woot!)

    Slide post (should not be here)

    Footer

    If I create any other post, or navigate to any other page on my site I get a broken slider and a 404 error message.

    Any ideas?

  4. marv51
    Member
    Posted 1 year ago #

    sorry, I or my keyboard are getting tired:
    cpt is short for "costume post type"
    and flsh was supposed to read flush
    (call $redirect->flush_redirect, or go to "Permalinks" hit save)

    I will have another look at that tomorrow, but you should definitely remove all the global $wp_rewrite; $wp_rewrite->flush_rules();s.
    Read http://codex.wordpress.org/Rewrite_API/flush_rules to better understand why.

    query_posts changes the main query, so when that template file you pasted gets loaded, it will show the slides. Now the question is why does it always get loaded?
    Maybe look at http://codex.wordpress.org/Template_Hierarchy

    I really like the idea of using a Custom Post Type for a slider.

  5. Adam
    Member
    Posted 1 year ago #

    I removed all the flush jazz - I figured that was a bad thing.

    Its not a template, its just in the header - which was moved to its own file and then called into a header via a function call, but still not a template file - don't see the use for one in this case -

    How ever I still see the slider populated with jazz in the custom post type, but then I also see that custom post being in the main loop - when it shoudlnt

    Can I somehow exclude specific posts types from a loop?

  6. Adam
    Member
    Posted 1 year ago #

    Fixed:

    <?php
    	$args = array( 'post_type' => 'slides');
    	$loop = new WP_Query( $args );
    	?>
    
                  <?php
                    while($loop->have_posts()) : $loop->the_post();
                  ?>
                  <li>
                    <?php the_content(); ?>
                  </li>
    
                  <?php endwhile;?>
    >

    Note the $args and the $loop - I had to tell it very specifically which ones to go through

  7. marv51
    Member
    Posted 1 year ago #

    That makes totally sense. Glad you figured it out.

    As an explanation: When you load any WordPress page, WordPress goes and fetches the required posts or pages. Then it decides what template to use based on the template hierarchy I linked above. Now it executes that template file. call to header, the file content, then a call for the footer.
    If your theme in the header calls query_posts the posts/pages WordPress initially fetched get switched for the posts/pages you requested with query_posts. Now in the main, it will just loop through this main query and show what ever is there. In that case still the slides.

    If your problem is fixed, please mark this thread as resolved.
    Edit: I'm sorry, I didn't see you already did.

    Marvin

Topic Closed

This topic has been closed to new replies.

About this Topic