Format dates in a WordPress plugin - wordpress

I'm modifying the Recent-Changes WordPress plugin to display dates. I can echo the date but can't format it; e.g., mm/dd/yyyy.
I'd like the post_modified date to be in mm/dd/yyyy.
I've tried--
echo '<li>'.$RecentChange->post_modified('m/d/Y').
-- but that caused the plugin to stop displaying posts, and generally broke the site.
Below is the relevant chunk from the plugin--
/* define full SQL request */
$rc_sql = "SELECT post_date, post_modified, post_title, ID FROM wp_posts WHERE ".$rc_content_sql." ORDER BY post_modified DESC LIMIT ".$rc_number;
global $wpdb;
echo $before_widget;
echo $before_title.$rc_title.$after_title.'<ul>';
$RecentChanges = $wpdb->get_results($rc_sql);
if ($RecentChanges)
foreach ($RecentChanges as $RecentChange) :
$rc_url = get_page_link($RecentChange->ID);
echo '<li>'.$RecentChange->post_modified.' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
endforeach;
echo '</ul>'.$after_widget;
$wpdb->flush();
}

Try
<?php
mysql2date('m/d/Y', $RecentChange->post_modified);
?>
See reference.

Assuming RecentChanges->post_modified is a PHP date or time, you could wrap it in PHP's date function and format it how you want.
date("m/d/Y", $RecentChanges->post_modified);
So, your line would look like this:
echo '<li>'.date("m/d/Y", $RecentChanges->post_modified).' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
It's likely that your code is breaking WordPress because the post_modified function is just a getter and doesn't take parameters.

Related

How to convert the date integer value of ACF plugin in WordPress?

I am using the jQuery Date Picker of WordPress ACF Plugin to make a custom Meta Box of a custom POST.
Now, when this Date Picker value is saved to the database table called xxx_postmeta I see that value is:
20190630
So then when I get that meta value using below code:
<?php echo date('F j, Y ', get_post_meta( get_the_ID(), 'article_1_pub_date', true)) ; ?>
then the date is showing wrong, Like this:
August 22, 1970
Is there any wrong or ACF issue?
I assume the date you saved is June 30, 2019. You can’t use PHP date() function on this string.
Convert to a date object and print:
$date = DateTime::createFromFormat('Ymd', '20190630');
echo $date->format('F j, Y ');

Paginate, search & sort WordPress $wpdb query for external table

I have a custom table (ERA_Data) in my WP database, which I'd like to add pagination, column sort & search to.
Currently the following code successfully grabs the data (over 4000 entries) & displays them;
<?php
global $wpdb;
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");
echo "<table>";
foreach($ERA_Data as $ERA_Data){
echo "<tr>";
echo "<td>".$ERA_Data->PartNo."</td>";
echo "<td>".$ERA_Data->Make."</td>";
echo "<td>".$ERA_Data->Carline."</td>";
echo "<td>".$ERA_Data->Model."</td>";
echo "<td>".$ERA_Data->Description."</td>";
echo "<td>".$ERA_Data->Start_Year."</td>";
echo "<td>".$ERA_Data->End_Year."</td>";
echo "</tr>";
}
echo "</table>";
?>
Just so you know, this is all new to me - so I'm happy to read up on further info, but if anyone can provide some code examples that may help - I'd be extremely appreciative.
With regards to pagination, I think 50 or 100 items per page would do.
I'd imagine sort order & search will be far more useful in any case.
I would juts work on this line for anything you need:
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");
so change it like this for showing only a 100, 50, etc. results
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 100;");
or
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 50;");
and for pagination, this link would help so much What is the best way to paginate results in SQL Server
for sorting, depending on your column, use this syntax
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data ORDER BY ***COLUMN NAME HERE***;");
Let me know if you have any questions!

Show post count in Wordpress

Is it possible to get the exact counter of each post for display in a theme I'm making? So for example if I have 100 posts, the last post will have a number of 100, the one before that: 99, and so on. I can't use the post's ID on this since this is always incremented by 2 (most of the time) making it highly inappropriate as a counter.
Try this, look at the results you will get the idea.
Don't forget the array starts with 0 not 1.
Each array item will contain the post ID. so if Item(key) nr 15 is the same as the current page ID, then the current item is #16(15+1)
<?php
global $wpdb;
$query = "SELECT `ID`
FROM `$wpdb->posts`
WHERE `post_status` = 'publish'
AND `post_type` = 'post'
ORDER BY `post_date` ASC
"
;
$count = $wpdb->get_col($query);
var_dump($count);
moar info: wpdb
Do look at the query if you agree with my order.
You'll have to use a "counter" there, something like this:
$i=1;
while(have_posts):
....
<h2><?php echo $i.' - ';?><?php the_title();?>
...
$i++;
endwhile;

Wordpress wpdb select from multipe tables

I have a three tables for my wordpress plugin.
videos = id, name
playlists = id, name
video_playlist = id, video_id, playlist_id
how do I get multiple results for multiple tables.
ie, I am busy editing a playlist and would like to display all videos in the playlist.
so the ID for the playlist you are viewing is passed and that is referenced against the video_playlist table to obtain all the video IDs.
Now to take it one step further I would like to also display the names for the Videos.
Here is what I currently have.
<?php if(isset($update)) {
$rows = $wpdb->get_results("SELECT * FROM $table_play_vid WHERE playlist_id = $update->id");
foreach($rows as $row){
echo $row->video_id;
}} ?>
Try something like this.
?php if(isset($update)) {
$rows = $wpdb->get_results("SELECT vp.video_id, v.name FROM $table_play_vid vp, videos v WHERE vp.playlist_id = $update->id and vp.video_id=v.id");
foreach($rows as $row){
echo $row->video_id." ".$row->name;
}} ?>
I think it's a common MySQL query.
SELECT thistable.column, thattable.column FROM thistable,thattable WHERE thistable.something = thattable.something

Show Only Pings (Pingbacks+Trackbacks) Number on Wordpress

Is there a way to display just ping counts (number) on Wordpress?
Actually there are comments_number function but that showing total count of comments, pingbacks and trackbacks.
The following code works on WordPress 2.9.1. It may work on other versions, but I only tested it against 2.9.1.
<?php
global $wpdb;
$post_id = get_the_ID();
$total_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback'");
$total_approved_pings = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback' and comment_approved = 1");
$post_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback' and comment_approved = 1 and comment_post_id = $post_id");
echo "The total number of pings on this site is $total_ping_count.\n";
echo "The total number of approved pings on this site is $total_approved_pings.\n";
echo "The total number of approved pings on this post is $post_ping_count.\n";
?>
The above code gives counts just for pingbacks. If you want trackbacks instead of pingbacks simply change comment_type = 'pingback' to comment_type = 'trackback' or if you want a combined count change it to comment_type IN ('pingback', 'trackback').
Not exactly sure what you want: only show pingbacks? If so, and I haven't tried it, but Template Tags/wp list comments « WordPress Codex shows listing pingbacks and options.

Resources