• I have created a custom meta box where the list of other post appears with id and name . Now the slug of the current post is being changed according to the last id of that list. For example I have created the following post: 1. post1 2. post2

    Now the slug I’m getting if I create a new post with title “Stackexchange” then the slug will appear post-3 instead of stackexchange. Again if I create a new post then it follows like post-4, post-5. I think the list of id I am population in the custom meta box is affecting the slug. How can I prevent that?

    I have also tried wp_reset_postdata(), but it is still influencing the slug. Here is the code from functions.php:

    `add_action(‘add_meta_boxes’, ‘add_place_metaboxes’);
    function add_place_metaboxes(){
    add_meta_box(‘main_parent’, ‘Select Parent Place’, ‘main_parent’, ‘places’, ‘advanced’, ‘high’);
    }
    function main_parent(){
    global $post;
    // Noncename needed to verify where the data originated
    echo ‘<input type=”hidden” name=”eventmeta_noncename” id=”eventmeta_noncename” value=”‘ .
    wp_create_nonce( plugin_basename(__FILE__) ) . ‘” />’;

    $post_id = $_GET[‘post’];
    $activities = 0;
    if(!empty($post_id)){
    $activities = (get_post_meta( $post_id, $key = ‘custom_parent’, $single = true ));
    }
    $args = array(
    ‘post_type’ => ‘places’,
    ‘order’ => ‘ASC’,
    ‘orderby’ => ‘post_title’,
    ‘post_status’ => ‘publish’
    );

    $aquery = new Wp_Query($args);
    echo ‘<select name=”custom_parent[]” id=”main-parent” multiple=”multiple”>’;
    if( $aquery->have_posts()): while($aquery->have_posts()): $aquery->the_post();
    if(!empty($activities)){
    if(in_array(get_the_ID(),$activities)){
    echo “<option selected value=”.get_the_ID().”>”.get_the_title(). “</option>”;
    }
    else{
    echo “<option value=”.get_the_ID().”>”.get_the_title(). “</option>”;
    }
    }
    else{
    echo “<option value=”.get_the_ID().”>”.get_the_title(). “</option>”;
    }
    endwhile; wp_reset_postdata(); endif;
    echo ‘</select>’;

Viewing 2 replies - 1 through 2 (of 2 total)
  • Nora

    (@nora0123456789)

    In the loop, you called the_post() method, so Global Var $post is changed I think.

    How about this?

    // Get Posts
    $post_list = get_posts( $args ); 
    
    foreach( $post_list as $post_data ) {
    
      // Exec with "$post_data" not with Global $post
    
      // In order to get the post data, you need the params for functions.
    
      // Or
      // ID: $post_data->ID
      // Title: $post_data->post_title
    
    }
    Thread Starter 47bulletproof

    (@47bulletproof)

    @nora Thanks alot. I used get_posts instead of WP_Query(), its working now.
    $aquery->the_post() was the problem but I was using specific variable rather than only the_post().
    Fought so many days with WP_Query. How can $aquery->the_post() change globar $post. Thats really surprising. If you have any other thoughts, please do share, it might be very helpful in future. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WordPress wrong slug, slug generates from previous post slug’ is closed to new replies.