Skip Navigation

[Gelöst] When to use a direct wpdb call

This support ticket is created vor 7 Jahre, 4 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by Oniromancer vor 7 Jahre, 4 Monate.

Assigned support staff: Waqas.

Author
Artikel
#333891

I am trying to:
understand the difference between using this

$var = $wpdb->get_var( 
    "
    SELECT meta_value 
    FROM $wpdb->postmeta 
    WHERE meta_key = 'wpcf-smthing'
    " 
);

or this

$var = types_render_field( "smthing", array("output"=>"html"));
#333902

Waqas
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

The $wpdb->get_var() is a direct approach, to retrieve the value of a column, which is as straight-forward as it is stored in the database. The get_var() can be used to retrieve information from any column of any table in the WPDB context.

While, types_render_field() is a function particularly defined by Types Plugin's API, which retrieves information from a Types custom field only. Also, this function prepends 'wpcf-' prefix automatically, with the specified slug of the custom field. Since Types stores every custom field as 'wpcf-{field-slug}' in the database. This function also sanitizes the data, which is dependent on the type of the field.

Both (get_var and types_render_field) are quite different in their nature. Please read more about get_var() at https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Variable, while for types_render_field() at https://toolset.com/documentation/customizing-sites-using-php/functions/

#333909

If I understood correctly the only practical difference, willing to do the same thing, is that the types function sanitises the data.

Now, how can I use this types function in a php that exists outside the wp installation?
for example, for a get_var call I can load wp-load.php before.

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

thank you.

#333927

Waqas
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, you are right, for that particular matter - both can be treated as same, but types_render_field() does some extra work to ensure the sanity of the data.

Unlike WPDB class, you will have to make the plugin executable in a non-WP environment. In this case, you may need to strip several things off the plugin, or may need alter the plugin heavily.

I suggest taking a look at the code of the plugin and find the types_render_field() function for an understanding. You can also find the dependencies within the function itself.

However, I recommend using WPDB class for a non-WP setup, since this class is loosely coupled - so that it can be used with other setups.

#333936

thanks