Skip to content

Sitemaps: wrap column names in backticks to prevent SQL errors with reserved words#48474

Open
shsajalchowdhury wants to merge 1 commit intoAutomattic:trunkfrom
shsajalchowdhury:fix/sitemap-backtick-column-names
Open

Sitemaps: wrap column names in backticks to prevent SQL errors with reserved words#48474
shsajalchowdhury wants to merge 1 commit intoAutomattic:trunkfrom
shsajalchowdhury:fix/sitemap-backtick-column-names

Conversation

@shsajalchowdhury
Copy link
Copy Markdown
Contributor

@shsajalchowdhury shsajalchowdhury commented May 3, 2026

Fixes #48202

Proposed changes

The get_sanitized_post_columns() method in Jetpack_Sitemap_Librarian returns column names without backtick quoting. If a wp_posts column name matches a MySQL reserved word (e.g. order), the generated SELECT query fails with a SQL error.

This PR wraps each column name in backticks to ensure compatibility regardless of column names.

Before:

return implode( ',', array_map( 'esc_sql', $columns ) );

Produces: ID,post_author,post_date,...,order (breaks SQL)

After:

return '`' . implode( '`,`', array_map( 'esc_sql', $columns ) ) . '`';

Produces: ID,post_author,post_date,...,order (works correctly)

Related product discussion/links

Does this pull request change what data or activity we track or use?

No. This change only affects how SQL column names are quoted in sitemap queries. No data collection, tracking, or privacy-related changes.

Testing instructions

  1. Add a custom column to the wp_posts table with a MySQL reserved word name (e.g. order):
    ALTER TABLE wp_posts ADD COLUMN `order` INT DEFAULT 0;
  2. Enable Jetpack Sitemaps (Settings > Traffic > Sitemaps).
  3. Trigger sitemap generation by visiting a sitemap URL (e.g. /sitemap.xml).
  4. Before this fix: A SQL error occurs because order is interpreted as the SQL ORDER keyword rather than a column name.
  5. After this fix: The query succeeds because the column name is properly backtick-quoted as `order`.
  6. Remove the test column: ALTER TABLE wp_posts DROP COLUMN order;

Alternatively, review the code change directly — it adds backtick wrapping to column names in the return value of get_sanitized_post_columns(), which is used in two SELECT queries in the same file.

…eserved words

When a wp_posts column name matches a MySQL reserved word (e.g. 'order'),
the generated SQL query fails. Wrapping column names in backticks ensures
compatibility regardless of column names.

Fixes Automattic#48202
@github-actions github-actions Bot added [Feature] Sitemaps [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels May 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 3, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖



Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: May 5, 2026
    • Code freeze: May 4, 2026

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions Bot added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. OSS Citizen This Pull Request was opened by an Open Source contributor. labels May 3, 2026
Copy link
Copy Markdown
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a proposal for a different approach here:
#48202 (comment)

I would recommend trying this out instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Sitemaps OSS Citizen This Pull Request was opened by an Open Source contributor. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

backtick column names

2 participants