Snippet Shortcodes
  • Welcome
  • Getting Started
    • Page
    • Quickstart
    • What is the Premium Plugin?
    • Creating your first shortcode
  • Features
    • Overview
      • Auto-insert into Header / Footer
      • Limit to Device Types
    • Your own Shortcodes
    • Ready-made Shortcodes
      • Premium
        • Database values by ID
        • WooCommerce Meta Fields
        • WordPress User fields
      • Core
    • CSV Import
  • Further Info
    • Pricing & getting a license
  • Release notes
  • Contact / Issues
  • Download Links
    • Core plugin
  • Premium Plugin
Powered by GitBook
On this page
Edit on GitHub
  1. Features
  2. Ready-made Shortcodes
  3. Premium

Database values by ID

Use this shortcode to fetch a value from a specific column in a MySQL table, based on a matching key in another column. You’ll need to specify both the column to retrieve data from and the column to search against using the provided key.

For example, the shortcode below executes the corresponding SQL query:

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]

Pseudo MySQL:

Select {column} from {table} where {column-to-search} = {key}

Actual MySQL:

Select user_login from wp_users where id=3

Shortcode Arguments

The shortcode supports the following arguments:

Argument
Description
Options
Example

table

MySQL table to search

text

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]

cache

If enabled, cache the results of the SQL query for the number of seconds specified by

True (default) or False

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d" cache=false]

cache-duration

Number of seconds the data should be cached for (before hitting the database again)

Number, default 3600 seconds (1 hour)

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d" cache=true cache-duration=60]

column

MySQL table column to return

text

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]

column-to-search

MySQL column to use in Where clause

text

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]

key

Value to compare against column-to-search

text

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]

key-format

To stop SQL injection, a format (either numeric or string) must be specified for the key

Either "%d" (for a number) or "%s" for a string

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]

message-not-found

Message to display if no value can be found for the given key.

Either blank or text

[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d" message-not-found="Could not find user"]

Enabling and disabling this shortcode

You can enable this shortcode in the WordPress admin by navigating to Snippet Shortcodes > Settings. Then, set the “Enable ‘db-value-by-id’ shortcode?” option to Yes.

The following filter can also be used to force the setting to always be "No".

add_filter( 'disable-ss-sc-db-value-by-id', function( $default ){ 
    return true; 
});
PreviousPremiumNextWooCommerce Meta Fields

Last updated 5 days ago