My code is duplicating my wp posts - wordpress

I have written some php code that loops through the published posts on my wp site and when the Expiry Date (new field I added to each post) is <= to today's date, the post status is changed to draft.
It does change the status to draft however it duplicates the post. So I end up with 2 of the same posts, both set to "Draft".
I just want the original post status changed and that's it.
Not sure what I'm doing wrong here.
The echo statements are just for me testing the code.
Below is some of my code:

Related

How to show original publish post date instead of last edited date in wordpress

I have just updated the post on my blog changed its category and now its showing the current date instead of publishing date. How to display the publish date instead of the revision/update one.
ED website
A password to access is letmein Thank you in advance.
I think you must keep in a database field the initial date, and then you must modify the template in order to display the initial date not the last. But if you go to the Gui that can edited the post and you know the date you can update manual the date for a specific post. In Wp you can put feature or older date to a post.

Wordpress Post Date, shows month by number and text

I am having troubles with my Worpress post date. As the picture shows, I can publish the post with 01-Jan etc. I only want to post the month with text, and no numbers. Any suggestions?
You mentioned in the comments that the date on the front end was displayed wrong. Therefore, I suggest you look at the PHP source that is creating that page.
It's possible that the date format has been explicitly set in there, especially if you are using a non-custom theme, so the format under 'Settings -> General' could be ignored.

Inserted Wordpress Post Shows Bad Date In RSS Feed

Our wordpress blog (currently version 3.4.2) has an external process that inserts posts directly into the database from a third party. I don't have the ability to change that, so don't bother telling me it shouldn't be done that way. I CAN change the insert statements though.
The posts show up and everything looks fine except that the RSS feed shows an invalid year (expecting 2013, instead getting -0001) in the date field:
<pubDate>Wed, 30 Nov -0001 00:00:00 +0000</pubDate>
The post_date field in the wp_posts table for the post shows the correct date, and on the site the post has the right date. It shows up in the right place chronologically.
If I manually edit the post using the control panel and change the date even by just one second and re-publish the post then it fixes the feed.
This problem causes the RSS feed to not validate, and it's breaking other things. Help!
There are 4 dates stored for each WP post: Post_date, post_date_gmt, post_modified and post_modified_gmt.
I recommend you to insert the same date for both post_date and post_modified and see if it works.

Query next days from today in wordpress

I have a creepy site running Wordpress 2.8.5. Its purposed to show events in my state (Bahia/Brazil).
Every event has it published date as the day of the event and the theme shows future posts bypassing WP's default to display until today.
It has a slide with some featured events and this is how it now selects what is to be shown in the slides:
$mes = date('n');
$ano = date('Y');
query_posts("
meta_key=dest_principal&
meta_value=1&
showposts=6&
year=$ano&
monthnum=$mes&
order=ASC
");
With this code the slide shows the first 6 posts in the current month. The problem I have is not being able to show the next 6 posts begining today. (to be clear, by today i mean the day of access)
I found this entry " wordpress query - next two events by metadata date " but could not translate it to my need.
It's not clear exactly what you're trying to do. Are you trying to show 6 posts starting from today and going back to the 6 previous posts? So essentially the 6 newest posts?
If so try this in place of query_posts...
$recent = new WP_Query("cat=3&showposts=6");
while($recent->have_posts()) : $recent->the_post();
You can use the 'cat=3' to display posts from a particular category, just change the '3' to your category id. Otherwise if you don't need to do this remove this 'cat=3&'.
BTW, I have a site focused on Bahia if you're interested in a link exchange get in touch.

Wordpress - Sorting posts by expiry date

I'm trying to show expired posts AFTER non-expired posts. How can I do this?
I've added a custom field "Expiration Date", in which I store the expiration dates in yyyy/mm/dd format. Problem is, if I order my results by this field, future expiry dates come first.
So I created a repeating cron-job which compares the dates and creates a secondary custom field "Expiration Date Passed" for posts whose dates have passed. I tried ordering by this field, but WP only shows posts with a value for this field - IE posts with no expiry date, or expiry dates in the future, don't show. So I tried auto-adding values '99999999' for any post which haven't expired yet. Problem is, WP can't order by custom field values THEN date- IE the first posts with value '99999999' are in a random order.
I also tried doing two queries for posts, one without expired posts, one with, then merging these two arrays. So the data is in the right order - but it screwed up WP's pagination.
Help, I'm running out of ideas!
Since you have an "Expiration Date Passed" custom field , you could first create two sets of Posts using that custom field in your get_posts arguments to differentiate between current & expired Posts
$meta_key and $meta_value
(string) (optional) Only show posts that contain a meta (custom) field with this key and value. Both parameters must be defined, or neither will work.
Default: None
extract from:
http://codex.wordpress.org/Function_Reference/wp_get_recent_posts
then you'll be able to sort each set the way you want
That might work, but I am trying to sort the posts on my category pages. Wp_get_recent_posts function is usually used for creating custom loops, not modifying 'the loop' in category (archive template) pages.
In the end I sorted it with this. I added this code to the top of my archive template:
global $query_string;
query_posts($query_string . "&orderby=meta_value&meta_key=Expiration Date Passed&order=DESC");
I created a "sort" custom field called "Expiration Date Passed". A cron job then looks to see whether the post has an expiration date. If it doesn't, or if the date is in the future, it puts the post's publish date + 20 years in the sort column. If the post's expiration has passed, it puts the post's publish date in the sort column. Thus it results in the order I was after:
1) Posts which haven't expired, in date order
2) Posts which have expired, in date order
Thought I would post that solution in case anyone else wanted to know.

Resources