Search code examples
phpwordpressloopspermalinksmeta-boxes

Wordpress custom meta box loop destroys post slug


Now this is a crazy one. So bear with me...

I have a custom meta box. Inside it I have a post loop, that prints out another custom post type titles. I got everything to work, save etc. But now from that loop, the first posts title jumps into the permalink of the post I am editing.

I am using new WP_query to call the loop inside the metabox.

$args = array( 'post_type' => 'meeskond');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
.........
<?php 
endwhile;

I have tried other ways to call the loop, nothing seems to remedy this. As I understand the permalink is being populated right after my script runs. And somehow $page_name is taken out of my meta box loop.

Seems to me, that wp-admin/js/post.js is doing this, but I cant think of any way to fix it. My brain is empty... Any pointers ?


Solution

  • I have found many answers, but found one that actually works.

    Just changed my loop call inside my meta box to this.

    global $post;
    
    $filtArgs = array(
        'post_type' => 'insert_brains',
    );
    $filts = get_posts($filtArgs);
    
    $filtData = get_post_meta( $post->ID, $filtID, true );
    
    foreach ( $filts as $filt ) : setup_postdata($filt);
    
        $filtID = 'filt_' . $filt->ID; 
        $filtTitle = $filt->post_title;
        ?>
    
        ....
    
    <?php endforeach;
    

    Big thanks to Gaz. It works like magic. Stole his solution from here.