I figured it out! Pretty simple actually.. You can replace post_date_gmt with ID depending on your needs. post_author != 1 will prevent the admin’s post from being deleted, so the menu links and pages will stay intact 🙂
function deleteOldestPost(){
global $wpdb;
$prefix = $wpdb->prefix;
$wpdb->query("DELETE
FROM ".$prefix."posts where post_author != 1 order by post_date_gmt asc limit 1");
}
add_action('publish_post', 'deleteOldestPost');
There will be some transient data, i.e. wp_term_relationships. Any of the database optimize plugins will get rid of them.
Related Posts:
- Function returning queried meta value based on current post ID
- Multipart/formatted MySQL query problem
- Is it good practice to use wpdb->query() function?
- Is there a WP function to get taxonomy name from taxonomy_term_id?
- Writing a function for WP Cron to run a SQL command daily
- sort title descending with title with number
- Remove posts that start with similar words (like a delete duplicate posts plugin)
- How do you properly prepare a %LIKE% SQL statement?
- Return only Count from a wp_query request?
- Is there a way to list all used/unused WP templates?
- Order posts (across the whole site) by metadata date
- Sort posts by Date (DESC) and by Title (ASC)
- How to Use Wildcards in $wpdb Queries Using $wpdb->get_results & $wpdb->prepare?
- wpdb get posts by taxonomy SQL
- Filter the query ONLY for the search results page
- Inserting Post Meta From SQL
- How to make an activities stream mixing posts and comments?
- SQL query equivalent to WP User Query
- Delete post revisions on post publish
- Are database queries created using WordPress filters protected from SQL injection?
- How to execute mulitple statement SQL queries using $wpdb->query?
- Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
- Query WordPress database by registered date and role
- How to get last updated row or ID in wordpress
- Removing the first image in content
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- exclude a category from a search on a specific page
- How to correctly pass values to wpdb->prepare()?
- example of build_query function?
- Why is variable not working on custom sql query using wpdb?
- Shortcode pagination not advancing
- WSoD being caused by this piece of code
- Advanced SELECT query with condtional statements
- Which action does wp_update_user triggers?
- Wp_query function to search from product_title ‘OR’ product tags name
- Dynamic dropdown from database table
- How do I see the mysql query generated by get_posts( $args )?
- Create a new query in function.php to filter blog posts
- Order by summing multiple values
- Create table from array with prepare
- Using $wpdb (WPDB class) ‘replace’ with multiple WHERE criteria problem
- $wpdb->insert() does not Insert record in a table
- WordPress SQL Issue not returning correct reselts
- Accessing two databases wordpress
- Cannot get wpdb data (Error in a simple fuction) [closed]
- wpdb COALESCE won’t work
- Make a SQL query with wpdb in WordPress
- Custom Query – Based on user input
- Custom WordPress Table wpdb
- Best practice to limit results in get_row()?
- Use $wpdb or other PHP script method to find/replace in WP database
- How to get INSERT errors from $wpdb?
- how to insert missing tags into the posts through mySQL?
- PHP -> SQL Query with Summing
- SQL query to set posts in bulk based on the post content
- Query Column of Specific ID from Database Table
- Valid SQL query return empty
- mysql query – how to escape apostrophe?
- get_terms (or tax_query) for term of current post?
- $wpdb->prepare affecting the query?
- prepare function sql safe method
- wp_usermeta – Read from database, but save function broken
- Updating with $qpdb->query() always returns 0 rows affected
- Why my query does not run with prepare
- shorthand syntax for custom fields
- mySQL queries are executed twice on wordpress website
- How to return count of custom post type posts with a specific custom field value via $wpdb?
- making php value numeric
- How do I query for posts by custom meta and those that have been stickied?
- Help with Related Posts Function
- WordPress Categories: Function using custom SQL to return array of specific category IDs
- SQL error with custom query
- How to avoid saving empty data to sql while using add_meta_box
- This code is supposed to only allow user to be authenticated if accountVerified is equal to 1, but it still allows user to be authenticated otherwise
- How can I connect to a second database and still be able to use wp functions like get_post_types()?
- Customising the default wordpress search functionality
- Any way to use FETCH_KEY_PAIR with $wpdb?
- Selecting specific query for offset
- Why isn’t comment validation working?
- SQL query to delete users with multiple meta keys and comments
- Shortcode with pagination advancing multiple queries
- WPDB delivers wrong results from complex queries
- Are there any best practices for creating a Like/Favourite feature in WordPress using custom MySQL tables and without any plugins?
- $wpdb post type and term query only works when there are no dashes or spaces in the term slug and title
- Fastest and most efficient SQL query to check if UID exists
- How can I display a query in a page?
- What to do to have access to WordPress functions
- How to make MySQL search queries with quotes
- $wpdb query for price in custom field value
- Database Query not working
- Run an update query in a function
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Mysql query and odd results
- Query is not work
- Sorting date results from a query
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- WPDB query suddenly not working
- How to cache results of of get_available_post_statuses() function as it is causing a slow query on a website with over 1 million posts?
- SQL errors when querying for something with apostrophes