WordPress.org

Ready to get started?Download WordPress

Forums

problem using get_posts inside a function. same code works properly outisde (3 posts)

  1. ben.IT
    Member
    Posted 1 year ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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/get_the_ID

  3. ben.IT
    Member
    Posted 1 year ago #

    thanks for your reply, but this doesn't fix the bug, still the same id returned.

    any idea ?
    ben

Topic Closed

This topic has been closed to new replies.

About this Topic