Title: problem using get_posts inside a function. same code works properly outisde
Last modified: August 20, 2016

---

# problem using get_posts inside a function. same code works properly outisde

 *  [ben.IT](https://wordpress.org/support/users/benit/)
 * (@benit)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/problem-using-get_posts-inside-a-function-same-code-works-properly-outisde/)
 * Hello,
    I’m experiencing a problem with the following snippet : It works properly
   as a script (case 1) but when I put the exact same code into a function body,
   it always prints the same id (case 2) !
 * Case 1 code :
 *     ```
       <?php
       require('./wp-blog-header.php');
   
       function br()
       {
       	echo '<br />'.'--------------------------------------------'.'<br />' ;
       }
   
       //return an associative array of all wp int post : master_translation_id => master_translated_modification_date
   
       	$args = array(
           'numberposts'     => 1000,
           'offset'          => 0,
           'orderby'         => 'post_date',
           'order'           => 'DESC',
           'post_type'       => 'post',
           'post_status'     => 'publish' ); 
   
       	$list_master_id_date =array() ;
       	$lastposts = get_posts( $args );
       	foreach($lastposts as $post)
       	{
       		setup_postdata($post);
       		the_title();
       		br();
       		//the_content();
       		the_id() ;
       		br();
       		//var_dump($post) ;
       		$id=the_id();
       		$post_custom_fields= get_post_custom($id) ;
       		var_dump($post_custom_fields);
       		br();
       		//echo 'master_translation_id : '.$post_custom_fields["master_translation_id"][0].'<br>';
       		//echo 'master_translated_modification_date'.$post_custom_fields["master_translated_modification_date"][0].'<br>';
   
       		$list_master_id_date[$post_custom_fields["master_translation_id"][0]]=$post_custom_fields["master_translated_modification_date"][0] ;
       	}
       	//var_dump($list_master_id_date);
   
       	//return $list_master_id_date ;
   
       ?>
       ```
   
 * Case 2 code:
 *     ```
       <?php
       require('./wp-blog-header.php');
   
       function br()
       {
       	echo '<br />'.'--------------------------------------------'.'<br />' ;
       }
   
       //return an associative array of all wp int post : master_translation_id => master_translated_modification_date
       function test()
       {
       	$args = array(
           'numberposts'     => 1000,
           'offset'          => 0,
           'orderby'         => 'post_date',
           'order'           => 'DESC',
           'post_type'       => 'post',
           'post_status'     => 'publish' ); 
   
       	$list_master_id_date =array() ;
       	$lastposts = get_posts( $args );
       	foreach($lastposts as $post)
       	{
       		setup_postdata($post);
       		the_title();
       		br();
       		//the_content();
       		the_id() ;
       		br();
       		//var_dump($post) ;
       		$id=the_id();
       		$post_custom_fields= get_post_custom($id) ;
       		var_dump($post_custom_fields);
       		br();
       		//echo 'master_translation_id : '.$post_custom_fields["master_translation_id"][0].'<br>';
       		//echo 'master_translated_modification_date'.$post_custom_fields["master_translated_modification_date"][0].'<br>';
   
       		$list_master_id_date[$post_custom_fields["master_translation_id"][0]]=$post_custom_fields["master_translated_modification_date"][0] ;
       	}
       	//var_dump($list_master_id_date);
   
       	//return $list_master_id_date ;
       }
   
       test() ;
       ?>
       ```
   
 * Case 1 output :
 *     ```
       international translation 2
       --------------------------------------------
       465
       --------------------------------------------
       465array(2) { ["master_translation_id"]=> array(1) { [0]=> string(4) "5119" } ["master_translated_modification_date"]=> array(1) { [0]=> string(19) "2012-08-09 09:12:11" } }
       --------------------------------------------
       international translation
       --------------------------------------------
       464
       --------------------------------------------
       464array(2) { ["master_translation_id"]=> array(1)
       ```
   
 * Case 2 output :
 *     ```
       international translation 2
       --------------------------------------------
       465
       --------------------------------------------
       465array(2) { ["master_translation_id"]=> array(1) { [0]=> string(4) "5119" } ["master_translated_modification_date"]=> array(1) { [0]=> string(19) "2012-08-09 09:12:11" } }
       --------------------------------------------
       international translation 2
       --------------------------------------------
       465
       --------------------------------------------
       465array(2) { ["master_translation_id"]=> array(1) { [0]=> string(4) "5119" } ["master_translated_modification_date"]=> array(1) { [0]=> string(19) "2012-08-09 09:12:11" } }
       --------------------------------------------
       ```
   
 * In my WP back office, I can see the two published posts with the IDs : 464,465.
   
   Does anybody can help me ? Thanks. Greetings, ben

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/problem-using-get_posts-inside-a-function-same-code-works-properly-outisde/#post-2954638)
 * this line might not work:
 *     ```
       $id=the_id();
       ```
   
 * try instead:
 *     ```
       $id=get_the_id();
       ```
   
 * [http://codex.wordpress.org/Function_Reference/the_ID](http://codex.wordpress.org/Function_Reference/the_ID)
   
   [http://codex.wordpress.org/Function_Reference/get_the_ID](http://codex.wordpress.org/Function_Reference/get_the_ID)
 *  Thread Starter [ben.IT](https://wordpress.org/support/users/benit/)
 * (@benit)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/problem-using-get_posts-inside-a-function-same-code-works-properly-outisde/#post-2954648)
 * thanks for your reply, but this doesn’t fix the bug, still the same id returned.
 * any idea ?
    ben

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘problem using get_posts inside a function. same code works properly outisde’
is closed to new replies.

## Tags

 * [function](https://wordpress.org/support/topic-tag/function/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)

 * 2 replies
 * 2 participants
 * Last reply from: [ben.IT](https://wordpress.org/support/users/benit/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/problem-using-get_posts-inside-a-function-same-code-works-properly-outisde/#post-2954648)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
