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)
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.