0

I want to use a metabox that allows the author to select the related post in all post edit pages.. The codes are working when publish the post, the meta is being saved.. But the post's slug is changing. For example, my post's url is "www.example.com/qwerty". After I publish the post by changing the related post, the url is "www.example.com/asdfgh-2".

"asdfgh" is previous post's slug. I am so confused, it is a very strange problem. I'm trying to solve it for days but i couldn't.. My metabox is below. Is it wrong to use a loop in a metabox?

<div class="meta-td">
          <?php

                                // The Query
                                $rb_query = new WP_Query( 'posts_per_page=1000' );

                                // The Loop
                                if ( $rb_query->have_posts() ) {
                                    echo '<select style="width:220px;" name="related-post">';
                                    echo '<option value="none"  >Select a post</option>';
                                    while ( $rb_query->have_posts() ) {
                                        $rb_query->the_post();
                                        $rid = get_the_ID();
                                        echo '<option value="';
                                        echo $rid;
                                        echo '"';
                                        if ( $saved_meta['related-post'][0]== $rid ){ echo ' selected'; }
                                        echo '>';
                                        echo get_the_title();
                                        echo '</option>';
                                    }
                                    echo '</select>';
                                    wp_reset_postdata();
                                } else {
                                    // no posts found
                                }
                            ?>
        </div>

Here is the metabox code enter image description here

Here is the save code enter image description here

2
  • What does your save_post code look like? Commented Jul 13, 2017 at 23:07
  • Sorry, I've just added the codes to the post.. Commented Jul 14, 2017 at 17:30

1 Answer 1

0

I think that I already found the problem. From wordpress get_posts documentation the have an example of get_post

<ul>
<?php

global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();?>

</ul>

And the problem is the variable $post inside the loop, I am not sure if maybe because it cause conflicts with the other global variable $post in the line #4.

So I changed my code from this:

$countries = get_posts(array(
    'post_type' => 'country',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order'   => 'ASC',
));    
?>
<p>
<!-- my custom value Select -->
<label for="location_country_id">Country:</label>
<select name='location_country_id' id='location_country_id'>
    <option value=""> -- choose Country -- </option>
    <?php 
    if ( $countries ):
        foreach  ( $countries as $post ) :  setup_postdata($post);
        $selected = '';

        if ( $current_country == $post->ID)
        {
            $selected = 'selected';                
        }
    ?>
        <option value="<?php echo esc_attr($post->ID); ?>" <?=$selected?>><?php echo esc_html($post->post_title); ?></option>
    <?php endforeach; 
        /* Restore original Post Data */
        wp_reset_postdata();
    endif;
    ?>
</select>
</p>

For this

$countries = get_posts(array(
    'post_type' => 'country',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order'   => 'ASC',
));    
?>
<p>
<!-- my custom value Select -->
<label for="location_country_id">Country:</label>
<select name='location_country_id' id='location_country_id'>
    <option value=""> -- choose Country -- </option>
    <?php 
    if ( $countries ):
        foreach  ( $countries as $country ) :  setup_postdata($country);
        $selected = '';
        //if($player->id ==  $item->title)
        if ( $current_country == $country->ID)
        {
            $selected = 'selected';                
        }
    ?>
        <option value="<?php echo esc_attr($country->ID); ?>" <?=$selected?>><?php echo esc_html($country->post_title); ?></option>
    <?php endforeach; 
        /* Restore original Post Data */
        wp_reset_postdata();
    endif;
    ?>
</select>
</p>

and No more break slug of the post.

I hope this help somebody else.

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.