Marc Ster
Marc Ster

Reputation: 2316

get post meta wordpress not working in plugin

EDIT: More Code.

Problem: I want to get the post meta of a post. It works fine for the case updated_post, but not for new_post and I just can't figure out why..

This is the function for the cases:

    function userpro_sc_new_post( $new_status, $old_status, $post ) {
    global $userpro_social;
    $exclude = userpro_sc_get_option('excluded_post_types');
    if ($exclude != ''){
        $exclude_types = explode(',',$exclude);
    } else {
        $exclude_types = array('nav_menu_item');
    }
    if (!in_array($post->post_type, $exclude_types )) {
        // new post
        if ( $new_status == 'publish' && $old_status != 'publish' ) {
            $user = get_userdata($post->post_author);
            $userpro_social->log_action( 'new_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
        }
        // updated post
        if ($new_status == 'publish' && $old_status == 'publish' ){
            $user = get_userdata($post->post_author);
            $userpro_social->log_action( 'update_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
        }
    }
}

And this is the code to run in the cases:

function log_action($action, $user_id, $var1=null, $var2=null, $var3=null) {
    global $userpro, $userpro_social;
    $activity = get_option('userpro_activity');
    $timestamp = current_time('timestamp');

    $status = '';

    switch($action){


        case 'new_post':
            $myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
            $status .= $myId;

            break;

        case 'update_post':
            $myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
            $status .= $myId;
            break;

            }

Like I said, update_post works so I can see the ID... new_post does not work. Why?

I simplified the code to run a bit, but it is still the same issue.

Please help!

Upvotes: 0

Views: 1644

Answers (3)

siiimple
siiimple

Reputation: 33

Try this:

$myId = (get_post_meta(get_the_ID(), 'wpex_post_video_oembed',true));

Upvotes: 0

Tristup
Tristup

Reputation: 3673

global $post;
$avriable_name=get_post_meta($post->ID, 'video_id', true);

Try the above code, global post will help to get the id for the post. If you didnt decalre it $post->ID will be blank and rest will not work.

Please let me know if you need further help.

Upvotes: 0

Anoop Asok
Anoop Asok

Reputation: 1337

You have to be aware of three things before using get_post_meta() in your plugins.

  1. You must declare global variables as global if any (eg: $wpdb).
  2. You have to get the post data in $post_id (eg: $post_id = $_POST['postid'];).
  3. Update the custom field value if needed (eg: update_post_meta($post_ID, 'video_id', true);).

Any of the above could be your problem. Please refer and try.

Upvotes: 1

Related Questions