You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python调用WordPress XMLRPC检查文章标题是否存在的问题

Solution for Checking Duplicate Post Titles via WordPress XML-RPC in Python

Hey there! I get that you're bulk publishing book posts to your library's WordPress site and need to avoid duplicates by checking for existing titles first. The snippet you have is a solid starting point—let's flesh out the missing find_id logic using WordPress's XML-RPC API to make this work.

Step 1: Use WordPress's wp.getPosts XML-RPC Method

WordPress’s XML-RPC API has a built-in wp.getPosts method that lets you query existing posts with filters. We’ll leverage this to search for posts matching your target book title.

Step 2: Implement the find_post_id_by_title Function

We’ll use Python’s built-in xmlrpc.client module (no extra packages required!) to connect to your WordPress site and check for duplicates. Here’s the full function:

import xmlrpc.client

def find_post_id_by_title(target_title, xmlrpc_url, username, password):
    try:
        # Connect to your WordPress XML-RPC endpoint
        client = xmlrpc.client.ServerProxy(xmlrpc_url)
        
        # Filter posts by title, and only fetch necessary fields to keep requests light
        filters = {
            'post_title': target_title,
            'post_type': 'post'  # Update this if you're using a custom "book" post type
        }
        fields_to_fetch = ['ID', 'post_title']
        
        # Fetch posts matching the title filter
        matching_posts = client.wp.getPosts(0, username, password, filters, fields_to_fetch)
        
        # Check for matches (case-insensitive by default—adjust if needed)
        for post in matching_posts:
            if post['post_title'].casefold() == target_title.casefold():
                return post['ID']
        
        # No matching post found
        return None
    except Exception as e:
        print(f"Error checking for duplicate title: {str(e)}")
        return None

Step 3: Integrate with Your Publishing Workflow

Now plug this function into your existing code to handle duplicate checks before publishing:

# Replace these with your site's actual details
XMLRPC_URL = "https://your-library-site.com/xmlrpc.php"
WP_USERNAME = "your-admin-username"
WP_PASSWORD = "your-app-password"  # Use an app password if 2FA is enabled on your site

# Example: Loop through your list of book posts
for book_post in your_book_posts_collection:
    post_title = book_post.title
    
    # Check for existing posts with the same title
    existing_post_id = find_post_id_by_title(post_title, XMLRPC_URL, WP_USERNAME, WP_PASSWORD)
    
    if existing_post_id:
        print(f"Sorry, we already have such a post (ID: {existing_post_id}) - Title: {post_title}")
        continue  # Skip publishing this duplicate
    else:
        # Proceed to publish the new post via XML-RPC
        try:
            client = xmlrpc.client.ServerProxy(XMLRPC_URL)
            new_post_data = {
                'post_title': post_title,
                'post_content': book_post.content,
                'post_status': 'publish',  # Use 'draft' if you want to review first
                # Add categories, tags, or custom fields here as needed
            }
            new_post_id = client.wp.newPost(0, WP_USERNAME, WP_PASSWORD, new_post_data)
            print(f"Successfully published: {post_title} (ID: {new_post_id})")
        except Exception as e:
            print(f"Failed to publish {post_title}: {str(e)}")

Key Tips to Keep in Mind:

  • Case Sensitivity: The example uses casefold() for case-insensitive matching (so "To Kill a Mockingbird" and "to kill a mockingbird" are treated as duplicates). Remove this if you need exact case matches.
  • Custom Post Types: If you’re using a custom post type for books (like book), update the post_type value in the filters.
  • Security: If your WordPress site has 2FA enabled, create an application password in your admin dashboard (Users > Profile > Application Passwords) instead of using your regular password.
  • Error Handling: The code includes basic error catching, but you can expand it to handle specific issues like authentication failures or connection errors if needed.

内容的提问来源于stack exchange,提问作者equallyhero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:04:07