0

EDIT: So I added the full code. What I'm trying to attempt is to get the post id into the metabox values for a publish immediate and scheduled post. It appears that I'm having trouble passing the id onto get_post_meta. Some of the stack exchange answers have helped to get me here (I believe I can now do an immediate publish via prfx_meta_usa/get_post_meta. But it seems something is stopping the scheduled/future posts from firing.

I've been reading a lot of the stack exchange topics on get_post_meta() but it appears that none of them have helped me. I've attempted multiple solutions here and none have really helped and I'm wondering if I'm just not getting it.

first its a new install of wordpress and it has twentyfourteen and this plugin as the only active ones.

I can echo $post->ID

I can echo get_the_ID()

When I attempt to use get_post_meta using either of those methods- I come up empty. If I click refresh (my page errors to white screen where I've var_dumped the variables and so I can hit refresh) it then populates with what I need it to do. (I've check the input field on the wordpress page, the database and it seems fine).

When I hardcode a previous post into get_post_meta it works like a charm.

 $meta_value = get_post_meta( $post->ID, 'meta-text', true );

// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
    echo $meta_value;

} else {
    echo "no meta value for meta-text";
    var_dump(get_post_meta($id ) );
    echo "<br> end of var_dump of meta post " ;
    $meta = get_post_meta($id );
    echo $meta['meta-text'][0]; //this is line 292
    echo "<br> This is something else ";
    echo $thePostID;

}

This outputs

silly test no meta value for meta-textarray(2) { ["_edit_last"]=> array(1) { [0]=>     string(1) "1" } ["_edit_lock"]=> array(1) { [0]=> string(12) "1400387873:1" } } 
end of var_dump of meta post 
Notice: Undefined index: meta-text in /var/www/wp-   content/plugins/remoteLinkedin/remotelinkedin.php on line 292

This is something else 705

When I click on refresh (and it asks me to resubmit the info) then the get_post_meta output looks like this

silly test
weeee we have lift off!
Wow
array(4) { ["_edit_last"]=> array(1) { [0]=> string(1) "1" } ["_edit_lock"]=> array(1) { [0]=> string(12) "1400387886:1" } ["meta-text"]=> array(1) { [0]=> string(3) "Wow" } ["theta-test"]=> array(1) { [0]=> string(3) "Now" } } 

Wow and Now are apart of the get_meta_post($post->ID) var_dump.

I'm frustrated as I spent most of my free time the last few days trying to figure out what was going on originally and I suspect somehow I'm messing up in a very silly and stupid way.

This is all being down in something that is attached as

add_action( 'transition_post_status', 'linkedin_intercept_all_status_changes', 10 , 3 );

I've tried wp_query, the_ID, get_the_ID so on

echo "<br> the post-> " . $post->ID;
    print "<br> the get id " . get_the_ID();

outputs

the post-> 705
the get id 705

It should be noted they are both on simultaneously and so I was under the impression that if you are in the loop you could get one and not the other???

I'm guessing that I'm not having get_post_meta jumping in at the right moment considering that it decides to work aftera resubmit...is the metapost data saving or something later than the post id...or is something stopping the post id from showing up with get_post_meta??

I apologize for a bit of rambling, I'm not an expert on wordpress and know beginning to intermediate PHP.

Side notes: I used http://themefoundation.com/wordpress-meta-boxes-guide/ as the basis for my metabox. I was using advance custom fields but I was getting the same issues.

EDIT: I think this is what is saving it?

/**
 * Saves the custom meta input
 */
function prfx_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
    update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}

}
add_action( 'save_post', 'prfx_meta_save' );

EDIT: This is the full code

/**
 * Adds a meta box to the post editing screen
 */
function prfx_custom_meta() {
add_meta_box( 'prfx_meta', __( 'Meta Box Title', 'prfx-textdomain' ), 'prfx_meta_callback', 'post' );
}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );

/**
 * Outputs the content of the meta box
 */
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>

<p>
    <label for="meta-text" class="prfx-row-title"><?php _e( 'Example Text Input', 'prfx-textdomain' )?></label>
    <input type="text" name="meta-text" id="meta-text" value="<?php if ( isset ( $prfx_stored_meta['meta-text'] ) ) echo $prfx_stored_meta['meta-text'][0]; ?>" />
</p>

<?php
}

/**
 * Saves the custom meta input
 */function prfx_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-text' ] ) ) {
    update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}

}
add_action( 'save_post', 'prfx_meta_save' );

function prfx_meta_use (){

 // Retrieves the stored value from the database
$meta_value = get_post_meta( get_the_ID(), 'meta-text', true );

// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
    echo $meta_value;
}
}








//****************************************************
//**************************************************************************


 /**
 * Adds a meta box to the post editing screen
 */
 function srfx_custom_meta() {
add_meta_box( 'srfx_meta', __( 'Meta Box Title', 'srfx-textdomain' ), 'srfx_meta_callback', 'post' );
}
 add_action( 'add_meta_boxes', 'srfx_custom_meta' );

/**
 * Outputs the content of the meta box
 */
function srfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'srfx_nonce' );
$srfx_stored_meta = get_post_meta( $post->ID );
?>

<p>
    <label for="theta-test" class="srfx-row-title"><?php _e( 'Example Text Input', 'srfx-textdomain' )?></label>
    <input type="text" name="theta-test" id="theta-test" value="<?php if ( isset ( $srfx_stored_meta['theta-test'] ) ) echo $srfx_stored_meta['theta-test'][0]; ?>" />
</p>

<?php
 }

 /**
  * Saves the custom meta input
  */
function srfx_meta_save( $post_id ) {

// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'srfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'srfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'theta-test' ] ) ) {
    update_post_meta( $post_id, 'theta-test', sanitize_text_field( $_POST[ 'theta-test' ] ) );
}

}
 add_action( 'save_post', 'srfx_meta_save' );

function srfx_meta_use (){

 // Retrieves the stored value from the database
$meta_value = get_post_meta( get_the_ID(), 'theta-test', true );

// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
    echo $meta_value;
    }
 }

Then creating the custom post

function my_custom_post_Linkedin() {
$labels = array(
    'name'               => _x( 'Linkedins', 'post type general name' ),
    'singular_name'      => _x( 'Linkedin', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'Linkedin' ),
    'add_new_item'       => __( 'Add New Linkedin' ),
    'edit_item'          => __( 'Edit Linkedin' ),
    'new_item'           => __( 'New Linkedin' ),
    'all_items'          => __( 'All Linkedins' ),
    'view_item'          => __( 'View Linkedin' ),
    'search_items'       => __( 'Search Linkedins' ),
    'not_found'          => __( 'No Linkedins found' ),
    'not_found_in_trash' => __( 'No Linkedins found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Linkedins'
);
$args = array(
    'labels'        => $labels,
    'description'   => 'Holds our Linkedins and Linkedin specific data',
    'public'        => true,
    'menu_position' => 3,
    'supports'      => array( 'title', 'editor', 'thumbnail' ),
    'has_archive'   => true,
);
register_post_type( 'Linkedin', $args );    
}
add_action( 'init', 'my_custom_post_Linkedin' );

This is my post transition function

function linkedin_intercept_all_status_changes( $new_status, $old_status, $post) {

//globals

global $post;
global $meta;
global $classes;

//$id = get_the_ID();
//$video_value = get_post_meta($id, '_post_video', true);
         // Retrieves the stored value from the database
   // $meta_value = get_post_meta( $id, 'meta-text', true );

// $linkedin_id = post->id;
 if ( ( $new_status == 'publish' ) &&  (get_post_type( $post ) == 'post')) {






 // Post status changed


require_once('db.php');
require_once('linkedin_3.2.0.class.php');

//http://stackoverflow.com/questions/11370356/having-trouble-with-the-oauth-verifier-for-offline-use-in-simple-linkedin-php
//http://wordpress.stackexchange.com/questions/135422/get-post-meta-doesnt-work
//http://codex.wordpress.org/Post_Status_Transitions
//http://themefoundation.com/wordpress-meta-boxes-guide/
//http://wordpress.stackexchange.com/questions/115852/why-is-my-get-post-meta-always-empty?rq=1
// ^ update_post_meta($post_id,'ch_event_date', 'Chris figured it out');
//

//*************************************
//
//*************************************

//beginning of the formal document
// I know * is not good but until I get it to work we'll leave it like this
$select = "SELECT * FROM wp_twitter WHERE oauth_provider='linkedin' AND wpid='1'";
$result = mysql_query($select);

if (mysql_num_rows($result)===0){

 echo "not connected";

}else{

$result = mysql_fetch_assoc($result);

//database variables
        $linkedin_oauth_provider = $result['oauth_provider'];
        $linkedin_oauth_token = $result['oauth_token'];
        $linkedin_oauth_token_secret = $result['oauth_secret'];
        $linkedin_uid =  $result['oauth_uid'];
        $linkedin_username = $result['username'];
        $linkedin_wpid = $result['wpid'];
        $user_ID = $result['wpid'];

}


// display constants
  $API_CONFIG = array(
'appKey'       => '*****',
  'appSecret'    => '*****',
  'callbackUrl'  => NULL 
  );



$linkedin = new LinkedIn($API_CONFIG);
$linkedin->setToken(array(
  'oauth_token' => $linkedin_oauth_token, 
  'oauth_token_secret' => $linkedin_oauth_token_secret
));



$id = print_r(get_the_ID(), TRUE);

//update_post_meta(get_the_ID() , 'meta-text', 'Chris figured it out');
//update_post_meta(get_the_ID() , 'theta-text', 'Chris figured it out');

$post_id = $post->ID;

prfx_meta_save( $post_id );
srfx_meta_save( $post_id );

prfx_meta_use ();
srfx_meta_use ();

$meta_value = prfx_meta_use (); //get_post_meta( $post->ID, 'meta-text', true );
$theta_value = srfx_meta_use ();//get_post_meta( $post->ID, 'theta-text', true );



$id = get_queried_object_ID();



$thePostID = get_the_ID(); //print_r ($_POST['ID'], true);

  $content = array();

   $content['comment'] =  $meta_value; //$thePostID; //get_field('comments_linkedin',$thePostID);//$comment;// get_field('linkedin_comments');

    $content['title'] = $post->post_title;//$_POST['stitle'];

   $content['submitted-url'] =  $theta_value; //$post_class; //get_field('submit_url_linkedin',get_the_ID());//get_the_ID(); //get_field('submit_url_linkedin',$thePostID); //$urllinkedin;//get_field("linkedin_description");//$_POST['surl'];

   $content['submitted-image-url'] = "rockitup";// $meta_value; // $post_class1; //get_field('image_url_linkedin',$thePostID);  //$image_url_linkedin;//get_field("image_url");//'hey'; //get_field('linkedin_image_url');//$_POST['simgurl'];

    $content['description'] = $post->post_content;//$_POST['sdescription'];

    $private = true;


  // share content
  $response = $linkedin->share('new', $content, $private);
  if($response['success'] === TRUE) {
    // status has been updated
   // header('Location: ' . $_SERVER['PHP_SELF']);




  } else {

global $post;


   // an error occured
    echo "Error sharing content:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($linkedin, TRUE) . "</pre>";
      echo "<br> This is the array  ";
      print_r($content);
      echo "<br> printing the post";
      print_r($post);
      echo "<br>";



    echo "<br> the post-> " . $post->ID;
    print "<br> the get id " . get_the_ID();

      echo "<br> var dumping post ";

var_dump($post);//test values in $post


      echo "<br> <br>" ;

//wp_reset_query();


// function of crazy

$id = print_r($post->ID, TRUE);
print $id;
echo "<br> silly test ";

 $meta_value = get_post_meta( $post->ID, 'meta-text', true );

// Checks and displays the retrieved value
if( !empty( $meta_value ) ) {
    echo "weeee we have lift off!"; 
    echo $meta_value;
    var_dump(get_post_meta($post->ID ) );
} else {
    echo "no meta value for meta-text";
    var_dump(get_post_meta($post->ID ) );
    echo "<br> end of var_dump of meta post " ;
    $meta = get_post_meta(get_the_ID());
    echo $meta['meta-text'][0];
    echo "<br> This is something else ";
    echo $thePostID;

}


  }  

 }
}  
add_action( 'transition_post_status', 'linkedin_intercept_all_status_changes', 10 , 3 );

This is the output (not including the linkedin print_r's as that's not the issue)

    This is the array Array ( [comment] => gasfg [title] => asdf [submitted-url] => [submitted-image-url] => rockitup [description] => asd ) 
printing the post

WP_Post Object ( 
 [ID] => 770 [post_author] => 1 
 [post_date] => 2014-05-18 15:56:38 
 [post_date_gmt] => 0000-00-00 00:00:00 
 [post_content] => asd 
 [post_title] => asdf 
 [post_excerpt] => 
 [post_status] => draft 
 [comment_status] => open 
 [ping_status] => open 
 [post_password] => 
 [post_name] => 
 [to_ping] => 
 [pinged] => 
 [post_modified] => 2014-05-18 15:56:38 
 [post_modified_gmt] => 2014-05-18 15:56:38
 [post_content_filtered] => 
 [post_parent] => 0 
 [guid] => http://www.rivallist.com/?p=770 
 [menu_order] => 0 
 [post_type] => post 
 [post_mime_type] => 
 [comment_count] => 0 
 [filter] => raw ) 

the post-> 770
the get id 770
var dumping post object
(WP_Post)#265 (24) { 
 ["ID"]=> int(770) 
 ["post_author"]=> string(1) "1" 
 ["post_date"]=> string(19) "2014-05-18 15:56:38" 
 ["post_date_gmt"]=> string(19) "0000-00-00 00:00:00" 
 ["post_content"]=> string(3) "asd" 
 ["post_title"]=> string(4) "asdf" 
 ["post_excerpt"]=> string(0) "" 
 ["post_status"]=> string(5) "draft" 
 ["comment_status"]=> string(4) "open"
 ["ping_status"]=> string(4) "open" 
 ["post_password"]=> string(0) "" 
 ["post_name"]=> string(0) "" 
 ["to_ping"]=> string(0) "" 
 ["pinged"]=> string(0) "" 
 ["post_modified"]=> string(19) "2014-05-18 15:56:38" 
 ["post_modified_gmt"]=> string(19) "2014-05-18 15:56:38" 
 ["post_content_filtered"]=> string(0) "" 
 ["post_parent"]=> int(0) 
 ["guid"]=> string(31) "http://www.rivallist.com/?p=770"
 ["menu_order"]=> int(0) 
 ["post_type"]=> string(4) "post"
 ["post_mime_type"]=> string(0) "" 
 ["comment_count"]=> string(1) "0" 
 ["filter"]=> string(3) "raw" } 

770
 silly test weeee we have lift off!gasfg

array(4) { 
["_edit_last"]=> array(1) { [0]=> string(1) "1" } 
["_edit_lock"]=> array(1) { [0]=> string(12) "1400428598:1" } 
["meta-text"]=> array(1) { [0]=> string(5) "gasfg" } 
["theta-test"]=> array(1) { [0]=> string(3) "sdf" } } 

 Warning: Cannot modify header information - headers already sent by (output started at /var/www/wp-content/plugins/remoteLinkedin/metabox.php:71) in /var/www/wp-admin/post.php on line 233

Warning: Cannot modify header information - headers already sent by (output started at /var/www/wp-content/plugins/remoteLinkedin/metabox.php:71) in /var/www/wp-includes/pluggable.php on line 896
  • wat? How are you saving your post meta? – Howdy_McGee May 18 '14 at 5:01
  • @Howdy_McGee I just edited the post to show what I think is saving it (It's straight from the tutorial and so I'm guessing this is it). – Cody May 18 '14 at 5:06
  • "wat?" Bit of Afrikaans coming through, lol – Pieter Goosen May 18 '14 at 5:07
  • 2
    it's not really clear where all of this code is. you have it in a function hooked to transition_post_status? but then you mention the loop? maybe back up a few steps, explain what you're trying to accomplish (what not how) and add the context of where this code is. – Milo May 18 '14 at 5:11
  • 1
    As @Milo implies, the context of your code is important. The code you have posted is out of context and disjointed. It is difficult to tell how it functions. Please try to clean this up and post all of the relevant code in context. – s_ha_dum May 18 '14 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.