• Resolved sergio_soares

    (@sergio_soares)


    Hello m8s.

    I Need to save a value of a Connection on a custom field.

    I did this:

    <?php
    function cria_campos() {
    global $post;
    $relacionadocasa = new WP_Query( array(
    	  'post_type' => 'casa-espectaculo',
    	  'connected' => $post->ID
    	) );
    	while ($relacionadocasa->have_posts()) : $relacionadocasa->the_post();
    
    	$local_evento = $relacionadocasa->post_title;
    
    	endwhile;
    update_post_meta($post->ID, 'local_evento', $local_evento);
    }
    add_action('publish_post', 'cria_campos');
    ?>

    Nothing is saved.

    What i’m doing wrong?

    The relationship is between post and ‘casa-espectaculo’ and i want to save to a custom field the value of the post title from casa-espectaculo post type.

    Help me pls. Sorry about some erros writing english

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author scribu

    (@scribu)

    Shouldn’t update_post_meta() be before endwhile (i.e. in the loop)?

    Also, I think you’d be better off using get_posts(). Remember to set 'suppress_filters' => false

    Thread Starter sergio_soares

    (@sergio_soares)

    Thanks Scribu =)

    Nice plugin, nice work m8 =)
    5*

    Thread Starter sergio_soares

    (@sergio_soares)

    <?php
    function cria_campos() {
    global $post;
    	$relacionadocasa = get_posts( array(
    	  'post_type' => 'casa-espectaculo',
    	  'connected' => $post->ID,
    	  'suppress_filters' => false
    	) );
    	foreach ($relacionadocasa as $relacionado) : setup_postdata($relacionado);
    		$local_evento = 'teste';
    		update_post_meta($post->ID, 'local_evento', $local_evento);
    	endforeach;
    }
    add_action('publish_post', 'cria_campos');
    ?>
    Thread Starter sergio_soares

    (@sergio_soares)

    on template i made like this:

    <?php
    // post
    global $post;
    $relacionadopost = new WP_Query( array(
    'post_type' => 'post',
    'connected' => $post->ID
    ) );
    if($relacionadopost) {
    echo '<h3>Informação Relacionada</h3>';
    }
    while ($relacionadopost->have_posts()) : $relacionadopost->the_post(); ?>
    
    <?php $local_evento = get_post_meta($post->ID, 'local_evento', true); ?>
    
    <?php echo $local_evento; ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php echo $casa; ?><br/>
    
    <?php
    endwhile;
    wp_reset_postdata();
    ?>

    no output is returned on variable $local_evento

    what i’m doing wrong?

    suppress filters means what? sorry if i a noob :/

    and sorry about the two posts. I missed the button lol

    ANY SOLUTIONS TO THIS PROBLEM AT ALL AS YET?

    Plugin Author scribu

    (@scribu)

    Looking at this again, I don’t see why you would need to store the titles as custom fields in the first place.

    See https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Plugin: Posts 2 Posts] update_post_meta not working as i expected’ is closed to new replies.