Fetching WordPress Categories with Python API

Tiempo de lectura: 3 minutos

Reading time: < 1 minute

Today we are going to learn how to fetch categories from the WordPress API.

Let’s create a python file named code.py with the following content:

import os
from dotenv import load_dotenv

import requests
import json
import random
from requests.auth import HTTPBasicAuth


def fetch_categories():

    load_dotenv()
    WORDPRESS_USERNAME = os.getenv("WORDPRESS_USERNAME")
    WORDPRESS_PASSWORD = os.getenv("WORDPRESS_PASSWORD")
    BASE_URL_WORDPRESS = os.getenv("BASE_URL_WORDPRESS")

    WP_categories = BASE_URL_WORDPRESS + "/wp-json/wp/v2/categories"

    auth = HTTPBasicAuth(WORDPRESS_USERNAME, WORDPRESS_PASSWORD)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    # Fetch categories list
    response = requests.request(
        "GET",
        WP_categories,
        headers=headers,
        auth=auth
    )

    return response.json()

Now we need to create a .env file to store the environment variables:

BASE_URL_WORDPRESS=WORDPRESS_URL
WORDPRESS_USERNAME=USER_
WORDPRESS_PASSWORD=PASS

Add your WordPress URL, username, and generate a password by visiting this link:

https://WORDPRESS_URL/wp-admin/authorize-application.php

Install the Python dotenv library to load environment variables:

RUN pip install python-dotenv

To run the code, use the following command:

python code.py

Reading time: < 1 minute

Today we are going to learn how to fetch categories from the WordPress API.

Let’s create a python file named code.py with the following content:

import os
from dotenv import load_dotenv

import requests
import json
import random
from requests.auth import HTTPBasicAuth


def fetch_categories():

    load_dotenv()
    WORDPRESS_USERNAME = os.getenv("WORDPRESS_USERNAME")
    WORDPRESS_PASSWORD = os.getenv("WORDPRESS_PASSWORD")
    BASE_URL_WORDPRESS = os.getenv("BASE_URL_WORDPRESS")

    WP_categories = BASE_URL_WORDPRESS + "/wp-json/wp/v2/categories"

    auth = HTTPBasicAuth(WORDPRESS_USERNAME, WORDPRESS_PASSWORD)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    # Fetch categories list
    response = requests.request(
        "GET",
        WP_categories,
        headers=headers,
        auth=auth
    )

    return response.json()

Now we need to create a .env file to store the environment variables:

BASE_URL_WORDPRESS=WORDPRESS_URL
WORDPRESS_USERNAME=USER_
WORDPRESS_PASSWORD=PASS

Add your WordPress URL, username, and generate a password by visiting this link:

https://WORDPRESS_URL/wp-admin/authorize-application.php

Install the Python dotenv library to load environment variables:

RUN pip install python-dotenv

To run the code, use the following command:

python code.py

Reading time: < 1 minute

Today we are going to learn how to fetch categories from the WordPress API.

Let’s create a python file named code.py with the following content:

import os
from dotenv import load_dotenv

import requests
import json
import random
from requests.auth import HTTPBasicAuth


def fetch_categories():

    load_dotenv()
    WORDPRESS_USERNAME = os.getenv("WORDPRESS_USERNAME")
    WORDPRESS_PASSWORD = os.getenv("WORDPRESS_PASSWORD")
    BASE_URL_WORDPRESS = os.getenv("BASE_URL_WORDPRESS")

    WP_categories = BASE_URL_WORDPRESS + "/wp-json/wp/v2/categories"

    auth = HTTPBasicAuth(WORDPRESS_USERNAME, WORDPRESS_PASSWORD)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    # Fetch categories list
    response = requests.request(
        "GET",
        WP_categories,
        headers=headers,
        auth=auth
    )

    return response.json()

Now we need to create a .env file to store the environment variables:

BASE_URL_WORDPRESS=WORDPRESS_URL
WORDPRESS_USERNAME=USER_
WORDPRESS_PASSWORD=PASS

Add your WordPress URL, username, and generate a password by visiting this link:

https://WORDPRESS_URL/wp-admin/authorize-application.php

Install the Python dotenv library to load environment variables:

RUN pip install python-dotenv

To run the code, use the following command:

python code.py
 (no incluyas el Tiempo de lectura). 

Reading time: < 1 minute

Today we are going to learn how to fetch categories from the WordPress API.

Let’s create a python file named code.py with the following content:

import os
from dotenv import load_dotenv

import requests
import json
import random
from requests.auth import HTTPBasicAuth


def fetch_categories():

    load_dotenv()
    WORDPRESS_USERNAME = os.getenv("WORDPRESS_USERNAME")
    WORDPRESS_PASSWORD = os.getenv("WORDPRESS_PASSWORD")
    BASE_URL_WORDPRESS = os.getenv("BASE_URL_WORDPRESS")

    WP_categories = BASE_URL_WORDPRESS + "/wp-json/wp/v2/categories"

    auth = HTTPBasicAuth(WORDPRESS_USERNAME, WORDPRESS_PASSWORD)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json"
    }

    # Fetch categories list
    response = requests.request(
        "GET",
        WP_categories,
        headers=headers,
        auth=auth
    )

    return response.json()

Now we need to create a .env file to store the environment variables:

BASE_URL_WORDPRESS=WORDPRESS_URL
WORDPRESS_USERNAME=USER_
WORDPRESS_PASSWORD=PASS

Add your WordPress URL, username, and generate a password by visiting this link:

https://WORDPRESS_URL/wp-admin/authorize-application.php

Install the Python dotenv library to load environment variables:

RUN pip install python-dotenv

To run the code, use the following command:

python code.py

Leave a Comment