How to show date with previous post link in Wordpress - wordpress

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.

Related

Wordpress admin: Get current open post (order) edit page meta value

We have a website running on woocommerce and we run a custom tax solution there. On frontend everything works perfect atleast in admin as we have set, that tax is calculated by store address, we get the basic store country into all of the orders. What we are trying to achieve is to replace this country in admin from current open order post meta. But we are unlucky and we are not getting any data. Is there any possiblity to get current open post meta value?
function custom_base_country($base_country ){
if (is_admin()){
global $woocommerce;
$order = $_GET['post'];
$country = get_post_meta($order->id, 'country_code', true);
$base_country = $country;
}
return $base_country;
}
add_filter('woocommerce_countries_base_country', 'custom_base_country', 10, 1);
Found out the answer by changing the code to $country = get_post_meta($order, 'country_code', true);

Can't get post ID and category using wp_insert_post_data

I would like to create post titles automatically. The post title is made from the category and category ID before the post is created in the database but does not get the category and ID.
Any help will be much appreciated, Thank You.
function my_post_titles($data, $postarr){
$postid = $postarr['ID'];
$postcategory = $postarr['post_category'];
$data['post_title'] = $postcategory."-".$postid;
return $data;
}
add_filter('wp_insert_post_data', 'my_post_titles', '99', 2 );
Couple of things to be aware of here: wp_insert_post_data is the filter used by wp_insert_post to allow changes to its first parameter, so to understand your problem, you should check how wp_insert_post works.
https://developer.wordpress.org/reference/functions/wp_insert_post/
Essentially, if the post you are creating a new post, then it should not have any ID (as it's not even created and saved to the database yet). To have access to the post ID, I suggest you use save_post hook rather than wp_insert_post. save_post is the action triggered after the post is created or updated. For example:
add_action('save_post', function($post_id) {
$title = get_the_title($post_id);
if ($title) {
return; // if the post has already has a title, then do nothing
}
// Otherwise, update the post title here
});
Also, $postarr['post_category'] is an array, not a string, so to get the correct information, you must convert it to string before concat it with the post_id.

Add meta field just after post is published

I'm trying to add a custom field of event_month when a post is published or saved. I'm using the save_post action and getting the contents of a custom field containing the date and trying to store this in a separate custom field with just the month. This works perfectly when saving a post that has already been created. I've shown my code below.
add_action('save_post', 'update_event_date');
function update_event_date($post_id){
$post_type = get_post_type($post_id);
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
if ($post_type == 'tribe_events'){
$month = date("m",strtotime($event_datee));
update_post_meta($post_id, 'event_month', $month);
}
}
The problem arises when creating a new post. I think this is because the action fires before the _EventStartDate meta has been created and therefore the month can't be taken from this.
The hook is firing correctly and as intended when saving/updating a post but doesn't correctly get the month from the meta when creating a new post.
I'd really appreciate it if someone could provide me with some guidance.
To access post meta passed together with yours, you can do something like this:
$event_datee = get_post_meta($post_id, '_EventStartDate', true);
foreach($_POST['meta'] as $meta){
if($meta['key'] == '_EventStartDate'){
$event_datee = $meta['value'];
}
};

How to remove the option to publish posts in the past with time stamp in wordpress?

I am trying to customize wordpress for my needs and I manged to do allot of changes but I can't figure out this one by my self.
I don't want to allow to authors to set up time stamp for the past, I do want this option to be enabled but only for posting new posts in the feature. So the minimum required date is today so if they pick a date before today they will get a message or something and the post won't progress.
Does anyone have any Idea how can I accomplish this with plugin or maybe a filter ?
Thanks to everyone who helps :)
http://codex.wordpress.org/Plugin_API/Action_Reference/save_post is what you need.
add_action('save_post', 'stop_publishing', 20);
function stop_publishing($post_id)
{
$post = get_post($post_id);
$post_date = $post->post_date;
$current_date = date("Y-m-d");
//do your math of date calculation
//user has set a date in the past, I am using an imaginary variable date_is_not_valid
if ( $date_is_not_valid ) {
$message = '<p>Please enter current or future date.</p>'
. '<p>Edit post</p>';
wp_die($message, 'Error - Incorrect Date!');
}
}

Add Meta Post function not working

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']);
?>

Resources