0

I registered a new post type "sidebars" so I can create different ones whenever I like.

function rh_create_sidebar_post_type() {
    $labels = array( 
        'name' => __( 'Sidebars' ),
        'singular_name' => __( 'Sidebar' ),
        'add_new' => __( 'New Sidebar' ),
        'add_new_item' => __( 'Add New Sidebar' ),
        'edit_item' => __( 'Edit Sidebar' ),
        'new_item' => __( 'New Sidebar' ),
        'view_item' => __( 'View Sidebar' ),
        'search_items' => __( 'Search Sidebars' ),
        'not_found' =>  __( 'No Sidebars Found' ),
        'not_found_in_trash' => __( 'No Sidebars found in Trash' ),
    );
    $args = array(
        'labels' => $labels,
        'has_archive' => false,
        'public' => true,
        'hierarchical' => false,
        'supports' => array(
            'title', 
            'editor', 
            'excerpt', 
            'custom-fields', 
            'thumbnail',
            'page-attributes'
        ),
    );
    register_post_type( 'sidebar_post', $args );
} 
add_action( 'init', 'rh_create_sidebar_post_type' );

Then on page/post admin screens, I load a meta box with a combo box of all the sidebars I have created

function load_sidebar_custom_meta_box( $post )
{
    $value = get_post_meta( $post->ID, '_rh_custom_sidebar', true );

    // The Query
    $args = array('post_type' => 'sidebar_post');
    $the_query = new WP_Query( $args );
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<select name="rh_custom_page_sidebar" id="rh_custom_page_sidebar">';
        echo '<option value="-1">No Sidebar</option>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $selected = '';
            if ( get_the_ID() == $value )  {
                $selected = 'selected';
            }
            echo '<option value="'. get_the_ID() . '" ' .$selected .'>' . get_the_title() . '</option>';
        }
        echo '</select>';
    }
    wp_reset_postdata(); //So custom sidebar post type doesn't overwrite main query
}

These two pieces work just fine. However, whenever I choose a sidebar and click save, it changes my page slug/URL to be whatever the URL is in the sidebar post type. And I can't change it back. No matter if I manually edit it at the top, and then click "OK" then "Update", it reverts back to the sidebar URL/slug.

For instance, the URL on the .com/about-us page I am trying to edit is now .com/default-sidebar and I can't change it back.

Here is my save method, not mentioning the post_name.

add_action( "save_post", "save_sidebar_post_page", 100 );
function save_sidebar_post_page( $post_ID )
{
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
        return $post_ID ;
    }
    global $post;
    if( isset( $_POST['rh_custom_page_sidebar'] )) {
        update_post_meta( $post_ID, '_rh_custom_sidebar', $_POST['rh_custom_page_sidebar'] );
    }
}

UPDATE

Noticed when I click "New Page" in the default Wordpress Page section, it seems to think it's my custom post_type of sidebar_post. Noticed when clicking on the Yoast Tab for Page Analysis, it had this error:

"No focus keyword was set for this sidebar_post. If you do not set a focus keyword, no score can be calculated."

So for some reason when it creates a new page, it thinks it's a sidebar post, so that must be happening when I Publish or Update, which would cause my issues with the permalink. However I am not seeing where in my code it would get these two things confused?

UPDATE 2

This seems to be happening after I set the custom sidebar the first time on the page. It works great and my permalink loads correctly on the front end. However, if I go back to edit the same page in the admin a second time, it looks like it loads the sidebar post_name in the admin in a hidden input field.

So when I update the page, that new post_name is getting submitted. Found the following embedded in the source after my first save. The value should be about-us.

<div class="inside">
<label class="screen-reader-text" for="post_name">Slug</label><input name="post_name" type="text" size="13" id="post_name" value="test-sidebar-post">
</div>

What it looks like is happening is my WP_Query is overriding the global $post variable. I see what you were saying now Milo. Trying it with get_posts

  • 1
    $post global is a bit different on the admin side. I don't think wp_reset_postdata works in that context. I would try using get_posts instead and not touching $post global. – Milo Oct 29 '15 at 21:12
  • The only time I reference global $post is during saving, and I was only doing that during debugging. It's not actually used during this process. – Nathan Oct 29 '15 at 21:18
  • you don't use it directly, but the_post() does. that's how the template tags get their data in the standard loop, and why you need to use wp_reset_postdata on the front end. – Milo Oct 29 '15 at 21:37
  • I am not using wp_reset_postdata on the front end; that's an is_admin method. I just added that recently b/c I was trying different things to try and figure why my slug was changing. It's not working though obviously. – Nathan Oct 29 '15 at 22:35
0

It was the WP_Query overwriting my main query. Using get_posts fixed the issue.

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.