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:
Pseudo MySQL:
Actual MySQL:
Shortcode Arguments
The shortcode supports the following arguments:
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".
Last updated