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!
You have to be aware of three things before using get_post_meta() in your plugins.
You must declare global variables as global if any (eg: $wpdb).
You have to get the post data in $post_id (eg: $post_id = $_POST['postid'];).
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.
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.
Try this:
$myId = (get_post_meta(get_the_ID(), 'wpex_post_video_oembed',true));
Related
When someone click on Add New post then a message will appear like "you are not allowed to create posts" and users also not allow to publish or draft the post.
I tried this code -:
function post_published_limit( $ID, $post ) {
$max_posts = 5; // change this or set it as an option that you can retrieve.
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post'); // get author post count
if ( $count > $max_posts ) {
// count too high, let's set it to draft.
$post->post_status = 'draft';
wp_update_post( $post);
}
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );
This code helps me not to publish the post but it creates draft of that posts .
I don't quite understand what you mean but I think this will do the trick.
function post_published_limit( $ID, $post ) {
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "administrator" ) ){
$max_posts = 5;
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post');
if ( $count > $max_posts ) {
$post->post_status = 'draft';
wp_update_post( $post);
}
}
}
I created this code to display the location from ACF that matches part of the URL and it works as expected.
<?php
$myurl= htmlspecialchars($_SERVER["REQUEST_URI"]);
$myexplodes = ( explode ('/', $myurl) );
$posts = get_posts(array(
'post_type' => 'my_vars',
));
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
?>
But when I try to place this code into a function nothing is displayed when I call it.
function local (){
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
}
I suspected that it is a scope problem with the vars but I have tried to make the vars global but had no luck.
The first one probably works because $post is a WordPress global variable, so I advise to use another variable name in your foreach.
For the thing that you want to do you should use also the post id in the get_field function call:
$value = get_field( "location", $article->ID );
I am having a problem with multiple if statements. I am using && but it seems to only work for the first statement.
The code is like such:
global $post;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
$user = wp_get_current_user();
if ( 3 <= count( $comment ) && $post->post_author == $user->ID) {
echo do_shortcode( '[button]' );
} else {
comment_form();
}
It basically stats that if there is less than 3 comments then show the comment form but if there is more than 3 and is the post author then show a button. The button shows but only if there are more than 3 comments. It doesn't check if it is only the post author or not, like I want it to.
What you are describing are two cases in which the button should be showed. So there have to be two ways to get into the if-block. You have to refactor your if-statement with the logical or-operator ||.
for example:
if($post->post_author == $user->ID || 3 <= count($comment)) {
echo do_shortcode( '[button]' );
} else {
comment_form();
}
I had to change the code like so to get it to work.
global $post,$current_user;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
get_currentuserinfo();
if ($post->post_author == $current_user->ID ) {
echo do_shortcode( '[button]' );
} elseif ( 3 <= count( $comment ) ) {
// blank
} else {
comment_form();
}
i want add a new field "title" on comment of wordpress, after insert the new input field in a default form of wordpress i added this in my function.php for save the title when new comment are submitted.
this is the code i use for save title:
function add_comment_meta_values($idcommento) {
global $post;
$idcommento= get_comment_ID();
$tipodipost= get_post_type($post->ID);
if( get_post_type($post->ID) == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
this code work only when remove the condition :
if( get_post_type($post->ID) == 'service') {}
and i don't understand why, i have already tried this condition in comment.php or in a footer with simple function like this
function test_function() {
if( get_post_type($post->ID) == 'service') { echo 'done'; }
}
add_action( 'wp_footer', 'test_function' );
and it's work, so i don't understand why don't work in my primary code, any idea ?
SOLVED MYSELF
THIS IS THE NEW CODE:
function add_comment_meta_values($idcomment) {
$comment_full = get_comment( $idcomment );
$idpost = $comment_full->comment_post_ID;
$typepost= get_post_type($idpost);
if( $typepost == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcomment , 'title_svz', $title, false);
} }
}
add_action ('comment_post', 'add_comment_meta_values', 10, 1);
Sometimes in Wordpress, depending on the Context, the global $post may give you unexpected result. Thus something like $post->ID may not point to the appropriate ID you are looking for. You may as well try inspecting the $post object to see if it is what you had expected; like so:
<?php
function add_comment_meta_values($idcommento) {
global $post;
// TRY DUMPING THE $post VARIABLE TO SEE WHAT YOU GET
var_dump($post->ID);
var_dump($post->post_type);
var_dump($post);
exit;
$idcommento = get_comment_ID();
$tipodipost = get_post_type($post->ID);
// ALTHOUGH THIS IS ESSENTIALLY THE SAME AS WHAT YOU DID
if( $post->post_type == 'service') {
if(isset($_POST['title_svz']) ) {
$title= wp_filter_nohtml_kses($_POST['title_svz']);
add_comment_meta( $idcommento , 'title_svz', $title, false);
}}
}
add_action ('comment_post', 'add_comment_meta_values', 1);
After the Inspection, you'd sure know where and what was not OK in your Code...
I am working on a plugin that sent an email when a post is published using
add_action('save_post','my_function');
my_function($post_id)
{
//do everything here
}
its working fine whenever a new post is published or its being updated from quick edit,
but the problem is that its not working when a post is schedule for future publish, for this I googled it, and find the following
add_action('publish_future_post', 'my_function');
this is the same function as used for above action,
I also found following action on some results,
add_action('future_to_publish', 'my_function');
but the last 2 action not working , mean its not sending any email,
can anyone help me to figure it out ,
#Andrew Bartel
here is my complete function,
function my_function($post_id) {
$post= get_post($post_id);
if ($post->post_type == 'post' && $post->post_status == 'publish') {
global $current_user;
get_currentuserinfo();
$usernamme = $current_user->user_login;
$email= $current_user->user_email;
$fname = $current_user->user_firstname;
$lname = $current_user->user_lastname;
$disname = $current_user->display_name;
$id = $current_user->ID;
$user = new WP_User($id);
if ( !empty( $user->roles ) && is_array( $user->roles ) )
{
foreach ( $user->roles as $role )
$user_role = $role;
$upper = ucfirst($user_role);
}
$email_post_options = get_option('email_post_options');
$adminemail =(!empty($email_post_options['adminemail'])) ? $email_post_options['adminemail'] : get_bloginfo('admin_email');
if(isset($email_post_options['rol']))
{
$msg = '';
$postdet = get_post($post_id);
$title = $postdet->post_title;
//$excerpt = substr($postdet->post_content,0,150);
$pdate = $postdet->post_date;
$permalink = get_permalink($post_id);
$price = get_post_meta( $post_id, '_my_meta_value_key', true );
$date = get_post_meta( $post_id, '_my_meta_date_key', true );
foreach($email_post_options['rol'] as $mailrol) // the roles which are saved from the plugin settings page, which is telling that who's role email will be received when a new post from the user is created.
{
if($mailrol==$upper)
{
$name = $fname.' '.$lname;
$usename = ($name!=' ')? $name : $usernamme;
$msg .='Full Name / Username : ' .$usename."\n";
$msg .='Title : '.$title."\n";
//$msg .='<p>Content : '.$excerpt.'</p>';
$msg .='Link = '.$permalink."\n";
$msg .='Price is = '.$price."\n";
$msg .='Added date = '.$date."\n";
$msg .='Published date = '.$pdate."\n";
$msg .='Total Posts : '.count_user_posts($id)."\n";
echo $msg;
if($email_post_options['npemail']==1)
{
wp_mail($adminemail, 'New Post', $msg);
}
}
}
}
} // end if
} // end function
its my function , if you have any confusion in this please let me know.
On line 3 you're checking the post_status of the post and explicitly checking for publish, which is only set for posts that (you guessed it) are published. When a post is scheduled to be published later, it's status is set to future. For your example, the first three lines:
function my_function($post_id) {
$post= get_post($post_id);
if ($post->post_type == 'post' && ($post->post_status == 'publish' || $post->post_status == 'future') ) {
Let me know if that works for you.