Create an array of custom post type links and names to use in a meta_box without changing the value of the post in edit

1

I'm working on developing a WordPress plugin using two types of post. The first type is created and updated normally, and I'm trying to use it to create a dropdown in the second type. However, when I call the function that generates the array for the post I'm working on, all other metaboxes' data is lost.

Function code that returns the array:

function array_integrantes($custom) {

    global $post;
    $old_post = get_post_custom($custom->ID);
    $type = 'clero';
    $args=array(
        'post_type' => $type,
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
    );

    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); 

            $integrantes[] = array(
                'nome_integrante' => get_the_title(), 
                'link_integrante' => get_permalink()
            );

        endwhile;
    }
    wp_reset_postdata();
    wp_reset_query();

    $post = get_post_custom($old_post->ID);
    return $integrantes;
}

Code where I call the function:

function paroquia_info($post) {
    $custom = get_post_custom($post->ID);
    $paroco = $custom["paroco"][0];
    $vigarios = $custom["vigarios"][0];
    $endereco = $custom["endereco"][0];
    $cep = $custom["cep"][0];
    $cidade = $custom["cidade"][0];
    $telefone = $custom["telefone"][0];
    $email = $custom["email"][0];
    $site = $custom["site"][0];
    $facebook = $custom["facebook"][0];
    $integrantes = array_integrantes($custom);
    echo'treco bugado<br><br><br>';   

    foreach ($integrantes as $row) {
        echo $row['nome_integrante'].'<br>';
        echo $row['link_integrante'].'<br>';   
    }
?>
    
asked by anonymous 25.09.2015 / 16:06

1 answer

1

You can not test your code, as it is not a Minimum, Full, and Verifiable example , but I think the probability it is too large that the problem is in WP_Query . And also:

  • global $post; will access the current post (if it is in the edition of a post / page), I do not understand why it is using;
  • The two calls to the get_post_custom() function appear to be of no use.

Basically it is: do not use the WP_Query function in plugins running in backend , the function get_posts() does not have danger none of influencing other queries that are happening, it simply queries and returns an array with the results. It does not have methods or auxiliary functions like have_posts , the_post or the_title , we just have to iterate on the object it returns and extract the information with get_the_title($ID) , get_the_permalink($ID , etc. >

The arguments are exactly the same as in WP_Query . Warning for $post->ID , $post->post_title , etc:

$args=array(
    'post_type'       => 'clero',
    'post_status'     => 'publish',
    'posts_per_page'  => -1,
    'caller_get_posts'=> 1
);
$posts = get_posts( $args );
$integrantes = array();
foreach ( $posts as $post ) 
{
    $integrantes[] = array(
        'nome_integrante' => $post->post_title, 
        'link_integrante' => get_permalink( $post->ID )
    );
}
// "var_dump" formatado com <pre> 
printf( '<pre>%s</pre>', print_r( $integrantes, true ) );
    
03.10.2015 / 02:47