{
  "id": "Bz9GCrL9GNF1TM4D",
  "name": "Auto-publish a WordPress post with a matching Pexels image",
  "tags": [],
  "nodes": [
    {
      "id": "c079093d-6318-4adf-b99e-dd1ee6564f05",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -48
      ],
      "parameters": {
        "width": 480,
        "height": 864,
        "content": "## Auto-publish a WordPress post with a matching Pexels image\n\n### How it works\n\nThis workflow manually starts a sample WordPress post publishing process, derives image search keywords from the post content, and uses Anthropic to refine them. It searches Pexels for a matching image, downloads the best result, uploads it to WordPress media, creates the post, and sets the uploaded image as the featured image.\n\n### Setup steps\n\n- Configure Anthropic credentials for the keyword generation node.\n- Add a valid Pexels API key to the Pexels HTTP request headers.\n- Replace the placeholder WordPress site URL with your actual WordPress REST API base URL.\n- Configure WordPress credentials or authentication headers for media upload, post creation, and featured-image update requests.\n- Adjust the sample post title and content before running, or replace the manual/sample input with your real content source.\n\n### Customization\n\nYou can change the prompt or code used to extract and clean keywords, adjust the Pexels search parameters such as orientation or image size, and modify the WordPress post status, categories, tags, or publication fields."
      },
      "typeVersion": 1
    },
    {
      "id": "4b37812b-e675-47d1-b89c-51af156983c1",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        496,
        -48
      ],
      "parameters": {
        "color": 7,
        "width": 496,
        "height": 320,
        "content": "## Start sample post\n\nManual execution starts the workflow and injects a sample post title and content for the rest of the automation."
      },
      "typeVersion": 1
    },
    {
      "id": "2b0c542e-66b1-4c72-a529-57000900caa3",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1104,
        -16
      ],
      "parameters": {
        "color": 7,
        "width": 784,
        "height": 304,
        "content": "## Generate image keywords\n\nThis cluster extracts an initial image query from the post, asks Anthropic to generate better search keywords, and cleans the result for API use."
      },
      "typeVersion": 1
    },
    {
      "id": "14251007-9adb-4b00-b204-7711567555d8",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2000,
        -48
      ],
      "parameters": {
        "color": 7,
        "width": 512,
        "height": 320,
        "content": "## Find and download image\n\nSearches Pexels with the generated keywords and downloads the selected image from the first returned photo result."
      },
      "typeVersion": 1
    },
    {
      "id": "1fd29bd1-5210-4602-8aad-1740e7814aa0",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2560,
        -48
      ],
      "parameters": {
        "color": 7,
        "width": 576,
        "height": 320,
        "content": "## Publish WordPress post\n\nUploads the downloaded image to WordPress media, creates the WordPress post, and patches the post to assign the uploaded media as its featured image."
      },
      "typeVersion": 1
    },
    {
      "id": "cd04314b-df36-4804-8ed2-1c49f38431a8",
      "name": "Manual Execution Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        544,
        112
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "64d99c60-6bad-409d-83d6-ba6376351bfc",
      "name": "Set Post Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        848,
        112
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "3ddde2d2-b76a-4605-8c79-237f82bc76ca",
              "name": "title",
              "type": "string",
              "value": "\"Best Hiking Trails in Banff National Park\""
            },
            {
              "id": "b5a9925a-0795-4576-992d-d01e042cdf5f",
              "name": "content",
              "type": "string",
              "value": "\"<p>Banff National Park offers some of the most breathtaking hiking trails in North America...</p>\""
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "fb9c7184-277b-4a85-9c4c-dfac963f38e7",
      "name": "Get Image Query from Content",
      "type": "n8n-nodes-base.code",
      "position": [
        1152,
        112
      ],
      "parameters": {
        "jsCode": "return [{ json: { imageQuery: $input.first().json.title } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "2c664d87-9466-479a-8848-b7dd03fe8f32",
      "name": "Refine Search Keywords",
      "type": "n8n-nodes-base.code",
      "position": [
        1744,
        112
      ],
      "parameters": {
        "jsCode": "const raw = $input.first().json.content[0].text;\nconst clean = raw.split('\\n')[0].replace(/[^\\w\\s]/gi, '').trim();\nreturn [{ json: { searchKeywords: clean } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "7eee8e93-5aa3-4520-91ef-af38befefb9d",
      "name": "Generate Pexels Search Keywords",
      "type": "@n8n/n8n-nodes-langchain.anthropic",
      "position": [
        1392,
        112
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "claude-haiku-4-5-20251001",
          "cachedResultName": "claude-haiku-4-5-20251001"
        },
        "options": {
          "maxTokens": 15
        },
        "messages": {
          "values": [
            {
              "content": "=Based on this article title, generate ONE short English search phrase (2-4 words) for a stock photo website like Pexels. Respond with ONLY that phrase on a single line. No newlines, no numbering, no punctuation, no alternatives.\n\nTitle: {{ $json.imageQuery }}"
            }
          ]
        }
      },
      "credentials": {
        "anthropicApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9db887f7-d4fe-4c74-b13d-58a10e1ffe00",
      "name": "Fetch Pexels Images",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2048,
        112
      ],
      "parameters": {
        "url": "https://api.pexels.com/v1/search",
        "options": {},
        "sendQuery": true,
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "queryParameters": {
          "parameters": [
            {
              "name": "query",
              "value": "={{ $json.searchKeywords }}"
            },
            {
              "name": "per_page",
              "value": "1"
            },
            {
              "name": "orientation",
              "value": "landscape"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "768e0832-ae7e-45ee-8e6e-5053b0259a10",
      "name": "Download Selected Image",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2368,
        112
      ],
      "parameters": {
        "url": "={{ $json.photos[0].src.large }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "36cb47db-b85a-423f-bb89-c2f834c6b736",
      "name": "Upload Image to WordPress",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2608,
        112
      ],
      "parameters": {
        "url": "https://your-site.com/wp-json/wp/v2/media",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "binaryData",
        "sendHeaders": true,
        "authentication": "predefinedCredentialType",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Disposition",
              "value": "=attachment; filename=\"{{ $binary.data.fileName }}\""
            }
          ]
        },
        "inputDataFieldName": "data",
        "nodeCredentialType": "wordpressApi"
      },
      "credentials": {
        "wordpressApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    },
    {
      "id": "8dd2e4ad-8d25-4a80-987f-915c2fbca0b7",
      "name": "Publish WordPress Post",
      "type": "n8n-nodes-base.wordpress",
      "position": [
        2800,
        112
      ],
      "parameters": {
        "title": "={{ $('Set Post Fields').item.json.title }}",
        "additionalFields": {
          "status": "draft",
          "content": "={{ $('Set Post Fields').item.json.content }}"
        }
      },
      "credentials": {
        "wordpressApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2ccce444-80d8-4393-ab82-9668814ad763",
      "name": "Set Post Featured Image",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2992,
        112
      ],
      "parameters": {
        "url": "=https://your-site.com/wp-json/wp/v2/posts/{{ $('Publish WordPress Post').item.json.id }}",
        "method": "PATCH",
        "options": {},
        "jsonBody": "={\n  \"featured_media\": {{ $('Upload Image to WordPress').item.json.id }}\n}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "wordpressApi"
      },
      "credentials": {
        "wordpressApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.4
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "121af6de-3898-4810-b972-4a4bdb228cf0",
  "nodeGroups": [],
  "connections": {
    "Set Post Fields": {
      "main": [
        [
          {
            "node": "Get Image Query from Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Pexels Images": {
      "main": [
        [
          {
            "node": "Download Selected Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish WordPress Post": {
      "main": [
        [
          {
            "node": "Set Post Featured Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Refine Search Keywords": {
      "main": [
        [
          {
            "node": "Fetch Pexels Images",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Selected Image": {
      "main": [
        [
          {
            "node": "Upload Image to WordPress",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Execution Trigger": {
      "main": [
        [
          {
            "node": "Set Post Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload Image to WordPress": {
      "main": [
        [
          {
            "node": "Publish WordPress Post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Image Query from Content": {
      "main": [
        [
          {
            "node": "Generate Pexels Search Keywords",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Pexels Search Keywords": {
      "main": [
        [
          {
            "node": "Refine Search Keywords",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}