What I am trying to do is each time a logged in user opens single.php I log a post meta activity. Essentially I want to know every time a logged in user reads a post. So I put this in single.php inside the loop and after having checked if user is logged in.
First I got just '6' (which is the post ID) in my post, so I added some echo to find which function was doing that and this is what resulted.
echo "before post meta<br />";
add_post_meta( the_ID(), 'post_read', (string)$current_user->ID );
echo "after post meta<br />";
prints out in my post:
before post meta
6after post meta
Why is add_post_meta printing '6' (the post ID)? How can I get rid of it? An incidentally when I:
$post_reads = get_post_custom_values('post_read', the_ID());
All I get is '6' again.
the_ID is printing out the post ID. Use get_the_ID to return it without printing.
You are using the_ID() in your function call. the_ID() displays the id to screen. You want to use get_the_ID() instead. This will give you a usable ID you can use for your function call.
Related
Guys I'm writting a wordpress site to run a knowldge base for our Service desk. As one person will be updating it i needed to have a field for who wrote the kb artical. I'm tring to add a custom field into my wordpress theme to display writtenby using Advance custom Fields. Now I'm using echo Knowledge Base plugin for knowldge base.
I've got as far add ing code below will display the text below the last up date value that plugin creates. However i cannot get it to put value from the custom field on the page after this. The plugin creates the page using php below the ive added the two lines as below.
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Writtenby:'.$wb.' ';
// LAST UPDATED ON
public static function last_updated_on( $args ) {
echo '' . esc_html( $args['config']['last_udpated_on_text'] ) . ' ' . EPKB_Utilities::get_formatted_datetime_string( $args['article']->post_modified, 'F d, Y' ).'';
$wb = get_post_meta($post->ID, 'Writtenby', true);
echo ' Last Update Written by:'.$wb.' ';
}
Advanced Custom Fields plugin use a little bit different system to store postmeta. Try to use get_field() instead get_post_meta()
If you have the ID
$customField = get_post_meta($my_id, "_mcf_customField", true);
However if you want to get the ID from the object:
$customField = get_post_meta($post_id->ID, "_mcf_customField", true);
After much more work looks like at the point the page was being created it had no reference to partical page not even a current one. They where writting to an array all artical numbers and info.
So by telling get_field which artical to get the writtenby field from it now displayed data and not blank
$wb= get_field('Writtenby', $args['article']->ID);
I'm using WordPress ACF plugin to store users' data.
The values I'm calling in template are as below:
echo $uid=get_current_user_id();
echo $lesson_order = get_field('lesson_order', 'user_'.$uid);
echo $last_lesson_time = get_field('last_lesson_time', "user_".$uid);
The values returned for admins are correct, but when logged in as a subscriber, the code returns empty values. I also checked user ids, which appear to be correct.
Can someone give me a solution to this. Thanks a lot.
I've tested both option on a local project and both were working but have you tried retrieving the user ID using this instead?
<?php
$current_user = wp_get_current_user();
echo "Current User ID " . $current_user->ID;
?>
If it still doesn't work, can you show a little more of your code?
You can also check that answer for more information : https://wordpress.stackexchange.com/questions/163407/get-current-user-id-returns-0
I want to get the URL of the Order Details page from where user can see their order details, because using some 3rd party API I send mail to the customer after the product is shipped.
I tried this code
$order = wc_get_order(109);
$public_view_order_url = esc_url( $order->get_view_order_url() );
echo $public_view_order_url; //http://example.com/my-account/view-order/109/
but the URL generated by ^^ above code only works for logged in customer. Is it possible to get a Public URL so that user don't have to logged in because most of the customer don't have any account. I know for security reason email id and invoice number is needed.
One solution that I think is by creating an custom page which will accept order_id, order_key and email_id in GET parameter and query it and display the result; the whole thing I have to create but is there any WooCommerce function/hook for this?
I googled it and also spend time in Woo doc but the result was negative,
Any help or suggation will be very helpfull for me.
Thanks.
Not sure if I'm missing something, but it sounds like what you want can be achieved with the Order Received URL:
$order->get_checkout_order_received_url()
This would give you something like https://example.com/checkout/order-received/12345/?key=wc_order_1ab2cd3ef4g, which is a direct link to the order receipt shown after a successfully processed order.
This link will show billing & shipping details, order details & payment method by default, and doesn't require that the customer has an account or for them to be logged in.
I couldn't find a way to do that so I just put a condition in my email. If they have an account, link to the order, if they don't encourage them to make an account for the next order.
<p><?php
$email_order_url =$order->get_view_order_url();
if ( $order->get_user() ) {
_e( "Your message with <a href='" . $email_order_number . "'>Order
Number'</a>.", 'woocommerce' );
} else {
_e( "Message to encourage them to create an account on the regular
accounts page" , 'woocommerce' );
}
Everything you need is in post meta, this works for me when in a loop that can access get_the_ID():
<?php $the_order_id = get_the_ID();
$the_order_key = get_post_meta($the_order_id, '_order_key', true); ?>
https://yoursiteurl.com/checkout/order-pay/<?php echo the_order_id; ?>/?pay_for_order=true&key=<?php echo $the_order_key; ?>
Otherwise, if you can replaced get_the_ID() with the Order ID in some other way (however you're getting 109 in your initial question), that would work as well.
When viewing a post, I want to show the date of the previous post underneath the previous/next post links but $post is not available in previous_post_link().
How can I carry forward the post date from the previous post as well as get the date for the next link?
Did a bit of searching on the web, but no result. Then I opened the file wp-include/link-tempalte.php.
On line 1327 you will find next_post_link which calls adjacent_post_link function.
This function again calls get_adjacent_post function to retrieve previous post data.
Looking at the source, you should be able to do the following:
$in_same_cat = false;
$excluded_categories = '';
$previous = true;
$post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
echo $post->post_date;
This is not tested, but I think it hould work.
I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>