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

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.

Related

group by category with 5 post of each category in wordpress REST api single Request

I am not sure if this has been answered before, but vigorous googling has not led me anywhere so far.
I have a Wordpress site where I would like to display all posts, as usual, ordered by date. However, instead of setting a limit on the total number of posts displayed, I would like to set a limit on each category in a single API request. I am using WP version 5.2.
What is the best, most clean and maintainable way to achieve this?
I would be very grateful for any help.

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

How do WordPress restore trashed posts to previous status?

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.

Wordpress custom post type list page large database

the custom post list page(where all posts are listed) is giving
Maximum execution time of 60 seconds exceeded error,
Increasing time to an extent gived the same result i.e. maximum execution time reached or memory allocation error
I have
No plugin activated,
wordpress default theme activated for debugging purposes
and still getting the same error
What I observed is for some reason wordpress is trying to show all custom posts(25,000 +) on the list page rather than showing 20 posts and than paginate the others, as no plugin is activated I am not sure what could be the reason behind this.
Is there any way I can observer what is causing this, I have added wp_debug to true in wp-config.php
Any help on this will be appreciated the custom post type have more than 25,000 posts
This is explained in the WordPress Codex. Quite simply, this is a symptom of WordPress fetching all "hierarchical" posts, along with their meta data:
Be careful, when choosing it for your custom post type - if you are
planning to have many entries (say - over 100), you will run into
memory issue. With this parameter set to true WordPress will fetch all
entries of that particular post type, together with all meta data, on
each administration page load for your post type.
The problem comes from the fact that, in order to display pages and their hierarchy, WP has to load all of them and then build the tree in memory.

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

Categories