How to move to trash wordpress posts with small content in python

Click to rate this post!
[Total: 1 Average: 5]

Introduction:
When working with a WordPress site, there may be instances where you need to extract specific content, such as posts with short descriptions, quickly and efficiently. One way to achieve this is by directly accessing the WordPress database using SQL queries. This approach not only offers better control over data retrieval but also helps avoid potential limitations imposed by hosting providers, such as restrictions on the number of pages that can be fetched via the WordPress REST API.

Summary:
In this solution, we will retrieve posts from the WordPress database that meet the following criteria:

  • The post content (description) is limited to 100 characters or less.
  • Only published posts are considered (drafts and trash are excluded).
  • We include a clickable link to the webpage for each post.

Here’s the Python code to achieve this:

import mysql.connector

# Connect to the database
conn = mysql.connector.connect(host='your-database-host',
                               database='your-database-name',
                               user='your-username',
                               password='your-password')

# Create a cursor
cursor = conn.cursor()

# Execute the SQL query
sql_query = """
    SELECT
        wp_posts.ID AS post_id,
        wp_posts.post_title AS post_title,
        wp_posts.post_content AS post_content
    FROM
        wp_posts
    WHERE
        wp_posts.post_type = 'post'
        AND wp_posts.post_status = 'publish'
        AND LENGTH(wp_posts.post_content) <= 500;
"""
cursor.execute(sql_query)

# Fetch all matching records
results = cursor.fetchall()

# Get the base URL of the WordPress site
base_url = 'https://www.electronics-engineering.com//?p='

# Process the results
for result in results:
    post_id, post_title, post_content = result
    post_url = f'{base_url}{post_id}'  # Construct the post URL
    print(f"Post ID: {post_id}")
    print(f"Title: {post_title}")
    print(f"Content: {post_content}")
    print(f"Link to Webpage: {post_url}")  # Include the link in the output

    # Set the post status to 'trash' to move it to the trash
    #update_query = f"UPDATE wp_posts SET post_status = 'trash' WHERE ID = {post_id};"
    #print(update_query)

# Close cursor and connection
cursor.close()
conn.close()

Please replace 'your-database-host', 'your-username', 'your-password', and 'your-database-name' with your actual database connection details. This code will allow you to efficiently retrieve and process posts that meet your specific criteria while bypassing any limitations on page retrieval imposed by your hosting provider.

if you comment out the line #43 and #44 and comment the lines #37 #38 #39 #40 you have direclty the result to be used to update the SQL. You can paste it this in your phpmyadmin

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x