How do WordPress restore trashed posts to previous status? - php

I'm am creating a simple administration system for one of my sites, and now I am creating a blog inspired by WordPress' posts system.
In my blog you can save it as either a draft or a published version.
And now I want to create a way, so when you delete a blog, it will change to "trashed" in the database. And when you restore it, it will be restored as the same type again, just like WordPress.
But how do WordPress restore it's trashed posts, to the original post_status? I have looked at the WordPress database, but I couldn't figure out, how it does it, cause the original post_status is replaced with "trash" when it is trashed and is not stored any other place in the table.
So how does it "remember" it's original post_status? Hope you understand what I'm asking :D
Sorry for not being a code-related question, but I couldn't find any answer any other places.
Thanks

To be short:
The previous post-status of a WordPress post, is stored in the wp_postmeta table.
In that table, the time for when the post were trashed, also are stored.
If you then restore the post, the rows of containing data for that specific post, are removed.
That should answer your question.

This is basic stuff to be frank. You should look at the wp_posts table and see the difference between a trashed and a published post ( and possibly, others too). Wordpress does this by changing the post_status column for that particular row of data. This way it knows the current status of the post concerned.
It might not be related to the answer but for those who are looking for answer to this question, should consider looking at the data source, i.e. the database of any system you are trying to learn... Be it Wordpress or Moodle.

All the posts, pages, and cpts are stored in {table_prefix}_postsin the database.
The posts, pages, or cpts which are trashed have the post_status as trash in some cases the permalink is also changed to __trashed
e.g; http://example.com/sample-page__trashed
To restore the post page, change the post_status back to published and remove __trashed from the permalink if it exists.
I recently fixed the issue on the site which was using the 'Frontend User Pro' plugin and the login page and reset password page was trashed. The site was not working and I fixed it using the above method.

Related

Wordpress posts missing but are still visible in database

I have an issue where all posts that are older than two days are missing. They are not in the trash in Wordpress, and if you go to the URL, it will come back with a 404.
I've tried disabling all plugins and repairing the tables in PHPMyAdmin as well as using the /wp-admin/maint/repair.php
I also have activated wp_debug and looked at the log; however there is nothing relating to this issue.
Now when I check the database in PHPMyAdmin - I can see all the posts there.
I've compared the posts that are currently published to the ones that are missing and the only difference I can see if that the post_status is set to "inherited" opposed to saying "published".
If I update the post_status of a missing post to "published" then edit the post in Wp-admin with the ID, eg, post.php?post=[missing post id]&action=edit it will show the missing post for about 2 seconds (including all the content) then redirect to an edit media page with every field blank.
I assume some plugin made this change, figuring out which one is the hard part.
Let me know if you need any more details from my end.
If a post is not viewable in WP dashboard but searchable in SQL table this could mean a revision has not been published (or the published revision has disappeared from the table). To turn on a revision see you must change post_type to 'post' and post_status to 'publish'
make a post viewable in phpmyadmin

Published posts are not query-able and not displayed in admin dashboard

I have encountered this problem many times. This problem only happen when I programmatically create/edit posts. They could be customs post or even Wordpress' default post. The symptoms could be:
The posts are not visible in admin dashboard. Post count of that custom post type still increase but no post is displayed in the list.
Created posts are not query-able, even when post_status is set to 'any'. Curiously when I set post_status to published it works.
This is a self-answered question. I encountered this problem several times before and always forget its cause. And searches hint to possibilities that many others are stuck without solution too.
TL;DR;: It's publish, not published.
The cause of this problem is because I when programmatically create new posts, I set its post_status to published. Common reason for the confusion is when you approve a post programmatically, switching post status from pending to published because it seem logical.
The reason those posts are showing in neither queries nor admin dashboard is because WP_Query's post_status => 'any' doesn't actually query for 'any' post status as we expect, but any registered post status. Since wordpress had no prior knowledge of the post status published, they're simply ignored by the queries.
Now here's to hope I don't forget what I wrote today again next year.

Wordpress - custom post types with custom fields, gone from admin menu

I followed the below tutorials in order to create a custom post type with custom fields.
https://www.youtube.com/watch?v=-r1b0wA98Ew
https://www.youtube.com/watch?v=6URWfMTsWPo
This worked great and I added lots of data using the forms I'd created and had the data displaying on the front end of the website.
A week ago, the custom post type disappeared from the Admin Menu but the the data seems to be intact from the front-end point of view. I posted a message in the CPT UI wordpress forum and got told to simply re-add the post type and re-link the data back up.
My question is HOW do I re-link the data back up? I have re-added the Custom Post type so its ls listed in the admin menu, but I need to be able to edit the custom posts I have already added. Can anyone help? Not getting much help from the Wordpress forum. Thanks
AS the data is intact in the front end it means the entries are still in database and nothing is lost.
It is correct way to just re-add the missing post-type and WordPress will now automatically re-link/re-connect your existing data, since WordPress is storing custom post type in the wp_posts table with post_type field value to your user-defined value.
Just make sure that the post-type name is same as you used earlier because slight mistake in spelling will lead to data not displayed in admin panel as expected.
You can also post further details which would be helpful to get actual problem found out.
#leetee this has 50/50 chance, experimental and since there is no way to reproduce this behaviour; try exporting your custom post types by using Tools -> Export and then see if you could Import them into a fresh WP install...

Wordpress: How to add additional folder for articles, besides draft and published/unpublished?

I automatically add articles as drafts to wordpress with script, moderator approves articles and publishes them or sends to some "storage" folder, so I to be able to train my machine learning algorithms on them. I've used to use folder "trash" for storing unapproved articles with
define( 'EMPTY_TRASH_DAYS', 999 );
in wp-config.php but it does not seem to work, articles are getting deleted anyway. So I think I should add additional category for articles besides "draft", "published", etc. I've read wordpress codex but it seems this topic isn't covered by it and googling yields nothing useful, only "101 wordpress tips" types of pages.
Another question, how to implement additional category in such way further updates won't break it? Or may be I'm just doing it all wrong?
Using custom post type will be the best solution in this situation. Here are some references for creating and managing custom post types in wordpress:
http://www.wpbeginner.com/wp-tutorials/how-to-use-custom-post-types/
http://blog.teamtreehouse.com/create-your-first-wordpress-custom-post-type
http://codex.wordpress.org/Post_Types

Wordpress Plugin - Saving simple data about a post

I'm trying to create a plugin in wordpress that allows you to age restrict certain posts and pages but not others. I looked for a plugin that does this and couldn't find one. I've got all the coding covered except I can't figure out how to instruct wordpress to save the state of the checkbox as data associated with that post or page. I dissected a couple of plugins that have similar functionality but am mystified by how their code works and it breaks when I try to modify it for my use.
Can someone point me to a tutorial that explains how data gets saved from the post edit page and how to access that data later?
check out post meta-data :
http://codex.wordpress.org/Custom_Fields
The basic idea is WP allows you to add additional fields to a post, and then retrieve it during the loop. Just use this field to test against at render-time and display alternate content, should (in this case) your post be restricted.
The codex is pretty thorough on the subject, and I'm sure google can fill in the gaps -
Hope that helps

Categories