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

Asked

Viewed 141 times

1

I am 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 data from other metaboxes is lost.

Code of the function 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>';   
    }
?>
  • You realize your method paroquia_info() wasn’t closed, right? And $integrantes[] = array ( should be $integrantes = array(

1 answer

1


You can’t test your code, because it’s not a Minimum, Complete and Verifiable Example, but I think the probability is too great that the problem is in the WP_Query.
And also:

  • the global $post; will access the current post (if it is in the editing of a post/page), I do not understand why you are using;
  • the two calls to the function get_post_custom() They don’t seem to be any use.

Basically it is: do not use the function WP_Query in plugins running on backend, the function get_posts() has not no danger at all to influence other queries that are happening, it simply queries and returns an array with the results. It has no methods or auxiliary functions such as 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. Attention to $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 ) );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.