• Hello,

    I got a problem, the function don’t go to the end… may be due to a big update (1300 posts) i don’t know.

    Can you tell me what’s wrong ?
    The function break all my custom post type… and the replace doesn’t work…

    add_action( 'get_header', 'my_update_posts' );
    function my_update_posts()
    {
        if( ! isset( $_GET['update'] ) ) {
            //echo' error';
            return;
        }
    
    	$taxonomies = array(
    	    'region',
    	);
    
    	$args = array(
    	    'orderby'           => 'name',
    	    'order'             => 'ASC',
    	    'hide_empty'        => true,
    	    'exclude'           => array(),
    	    'exclude_tree'      => array(),
    	    'include'           => array(),
    	    'number'            => '',
    	    'fields'            => 'all',
    	    'slug'              => '',
    	    'parent'            => '',
    	    'hierarchical'      => true,
    	    'child_of'          => 0,
    	    'get'               => '',
    	    'name__like'        => '',
    	    'description__like' => '',
    	    'pad_counts'        => false,
    	    'offset'            => '',
    	    'search'            => '',
    	    'cache_domain'      => 'core'
    	); 
    
    	$terms_region = get_terms($taxonomies, $args);
    
    	//var_dump($terms_departement);
    
    	//die();
    
    	foreach( $terms_region as $term_region )
    	{
    
        $args = array(
        	'post_type' => 'visite',
        	'posts_per_page' => -1,
        	'post_status' => 'any',
        	'orderby' => 'ASC',
        	'tax_query' => array(
    					array(
    						'taxonomy' => 'region',
    						'field' => 'slug',
    						'terms' => $term_region->slug,
    						)
    					),
    		);
    
        $posts = new wp_query( $args );
    
    	$posts = $posts->posts;
    	//var_dump($posts);
    	//exit;
        foreach( $posts as $post )
        {
            //setup_postdata( $post );
            //$value = $post->post_content;
            //var_dump($post);
            //die();
            $value = get_post_meta( $post->ID, 'description_visit_factory', true );
            if($value != ''){
            //var_dump($value);
            //exit;
    
            wp_update_post( array(
                'ID' => $post->ID,
                'post_content' => $value,
            ) );
    
            echo $post->post_title.' : ok ! <br/>';
    		//die();
    		} else {
    		echo 'NO VALUE !!!';
    		}
        }
    
    		echo '##### '.$term_region->name .' : done !<br/><br/>';
    
    		wp_reset_postdata();
    
    	}
    
    	//die();
    
        echo '###### update complete! #####';
    
        die();
    }

    Thank you for help

    Pop

Viewing 2 replies - 1 through 2 (of 2 total)
  • Where does it break and what error (if any) are you seeing?

    Just for the future, saying “doesn’t work” won’t get a lot of help as it’s almost impossible to tell what’s wrong without a lot more information.

    Thread Starter popsantiago

    (@popsantiago)

    Hello catacaustic

    Thanks for reply.

    The fact is i don’t know what’s the problem… I’m not really a dev.
    I find a function for update post_content and i take it.

    foreach( $posts as $post )
        {
            setup_postdata( $post );
            $value = get_post_meta( $post->ID, 'description_visit_factory', true );
    
            wp_update_post( array(
                'ID' => $post->ID,
                'post_content' => $value,
            ) );
    
        }

    With my 1300 posts i had a memory trouble (1024Mo) so i try to do by bloc with my custom tax query.

    foreach( $terms_region as $term_region )
    	{
    
        $args = array(
        	'post_type' => 'visite',
        	'posts_per_page' => -1,
        	'post_status' => 'any',
        	'orderby' => 'ASC',
        	'tax_query' => array(
    					array(
    						'taxonomy' => 'region',
    						'field' => 'slug',
    						'terms' => $term_region->slug,
    						)
    					),
    		);
    
        $posts = new wp_query( $args );

    Even with this the update seems didn’t go to the end because i’ve 23 regions but at the end >> ###### update complete! #####
    but the first region loop
    echo ‘##### ‘.$term_region->name .’ : done !

    ‘;
    doesn’t appear for each region.

    And posts ”updated” (in the ‘done !’ regions) didn’t update and in the taxonomy, posts are not anymore tag with the taxonomy ‘region’…

    Any idea ?

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Replace my content by a custom field’ is closed to new replies.