Skip to main content

wpypress

wpypress is a Python library designed for interacting with the WordPress REST API. It simplifies the process of managing posts, pages, media, categories, tags, WooCommerce products and SEO settings on WordPress sites.

Features

  • Post Management: Create, update, delete, and list posts.
  • Page Management: Create, update, delete, and list pages.
  • Media Management: Upload, update, delete, and list media items.
  • Category & Tag Management: Create, update, delete, and list categories and tags.
  • SEO Management: Manage SEO settings using the Yoast SEO plugin.
  • Ecommerce products: Manage products and product categories using WooCommerce APIs.

Installation

To install wpypress, simply use pip:

pip install wpypress

Usage

Initialization

To start using the library, first, initialize the WPClient with your WordPress site credentials:

from wpypress import WPClient

wp = WPClient(
    base_url="https://your-wordpress-site.com",
    username="admin",
    password="your-password"
)

Posts

Fetch Posts

Retrieve the first page of posts:

posts, pagination = wp.posts.list(params={'per_page': 10, 'page': 1})
print(f"Total posts: {pagination['total']}")
print(f"Total pages: {pagination['total_pages']}")
print(f"Current page: {pagination['page']}\n")

Fetch a Post

Retrieve a post by ID:

post = wp.posts.get(200) # WordPress Post ID
print(f"Post Title: {post['title']['rendered']}")

Retrieve a post by slug:

post = wp.posts.get(slug='my-post-slug') # WordPress Post Slug
print(f"Post Title: {post[0]['title']['rendered']}")

Create a Post

Create a new post with categories and tags:

post = wp.posts.create(
    title="My New Post",
    content="This is the content.",
    status="publish",
    categories=[2],
    tags=[5, 6],
    featured_media=15
)

Delete a Post

To delete a post, use:

wp.posts.delete(123, force=True)

Pages

Create a New Page

To create a new page:

page = wp.pages.create(
    title="About Us",
    content="This is our about page.",
    excerpt="About page",
    status="publish"
)

Media

Upload an Image

To upload a media item, execute:

media = wp.media.upload(
    file_path='./images/photo.jpg',
    title='My Photo',
    alt_text='A beautiful photo',
    description='Uploaded via REST APIs'
)

Categories

Create a Category

new_category = wp.categories.create(
    name="Tech News",
    slug="tech-news",
    description="Technology related articles"
)

Tags

Create a Tag

new_tag = wp.tags.create(
    name="Python",
    slug="python",
    description="All posts about Python"
)

SEO

Update Post SEO (Yoast SEO)

Ensure your site has the Yoast SEO plugin activated to manage SEO settings easily:

if wp.seo.is_yoast():
    wp.seo.update(
        post_id=456,
        title="Page SEO Title",
        description="Meta description for the page.",
        og_title="OpenGraph Title for Page",
        og_description="OpenGraph Description for Page",
        type="page"
    )

Products

Check if WooCommerce is Installed

Before performing operations related to WooCommerce, you can check if WooCommerce is installed on your WordPress site:

if wp.products.isWoocommerce():
    print("WooCommerce is installed.")
else:
    print("WooCommerce is not installed.")

Fetch Products

Retrieve the list of products:

products = wp.products.list(params={'per_page': 10, 'page': 1})
print(f"Total products: {len(products)}")

Fetch a Product

Retrieve a product by ID:

product = wp.products.get(100) # WooCommerce Product ID
print(f"Product Name: {product['name']}")

Create a Product

Create a new simple product:

product = wp.products.create(
    name="New Product",
    type="simple",
    regular_price="19.99",
    description="This is a simple product."
)

Update a Product

Update an existing product:

updated_product = wp.products.update(
    product_id=100,
    regular_price="24.99",
    description="Updated product description."
)

Delete a Product

To delete a product:

wp.products.delete(100, force=True)

Product Categories

The ProductCategoriesEndpoint allows you to manage WooCommerce product categories via the WooCommerce REST API.

List Product Categories

Retrieve a list of product categories with optional filtering and pagination:

categories, pagination = wp.product_categories.list(params={'per_page': 10, 'page': 1})
print(f"Total categories: {pagination['total']}")
print(f"Total pages: {pagination['total_pages']}")
print(f"Current page: {pagination['page']}\n")

Fetch a Product Category

Retrieve a product category by ID:

category = wp.product_categories.get(25) # WooCommerce Category ID
print(f"Category Name: {category['name']}")

Create a Product Category

Create a new product category:

new_category = wp.product_categories.create(
    name="Accessories",
    slug="accessories",
    description="Various fashion accessories"
)

Update a Product Category

Update an existing product category:

updated_category = wp.product_categories.update(
    category_id=25,
    description="Updated description for accessories"
)

Delete a Product Category

To delete a product category:

wp.product_categories.delete(25)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

Release: 0.2.0

Add support for yoast_wpseo_opengraph-image in the SEO endpoint. Add support for WooCommerce products and product categories.

Release: 0.1.3

Add support for slug in get method for posts and pages.

Metadata

Release files for wpypress 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wpypress 0.2.0
File Size Uploaded Provenance Actions
wpypress-0.2.0.tar.gz 9.7 kB Without provenance Details for wpypress-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wpypress 0.2.0
File Interpreter ABI Platform Actions
wpypress-0.2.0-py3-none-any.whl Python 3 none any Details for wpypress-0.2.0-py3-none-any.whl

Total release size: 22.5 kB

Release files / wpypress-0.2.0.tar.gz

Download URL wpypress-0.2.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5a834950373735d52dc56026eb144e14986351d01cc557fb083c5fbb361f62c2
BLAKE2b-256 checksum
How to use checksums
67498d5d9296179baf79a8d79df83ead9255e01c45a26a771035657fa9dfe9b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / wpypress-0.2.0-py3-none-any.whl

Download URL wpypress-0.2.0-py3-none-any.whl
Size 12.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5aedf8099c4e8f4b704c7b41a4e048b3352dd7b3bb0d760282b43b7b7c8b955c
BLAKE2b-256 checksum
How to use checksums
b0cfdb528d6eb5f8521cc999d4937e095e9c26e8528672866eb70a1326ac05b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for wpypress 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page