Skip to main content Skip to course navigation

Data access – get_option(), get_userdata(), and get_posts()

So far, you have been reading real WordPress data directly from the database with $wpdb. That was useful because it helped you see where the data actually lives.

Now comes the next mindset shift: in real WordPress development, you often do not query core tables directly for common tasks. Instead, you use WordPress functions that already know how to fetch that data in the normal WordPress way.

For this lesson, the three main functions are:

  • get_option()
  • get_userdata()
  • get_posts()

get_option() is the common way to read a value from the options table. Instead of manually querying wp_options with $wpdb, you usually ask WordPress for the option by name.

get_userdata() is a common way to retrieve one user’s data. Instead of querying the users table directly, you pass a user ID and WordPress gives you that user’s object.

get_posts() is a common way to retrieve posts. Instead of manually writing a SELECT query against the posts table, you pass an array of arguments, such as post type, post status, and number of posts, and WordPress handles the query.

The important lesson here is not that $wpdb is wrong. It is that WordPress has layers.

At the lower layer, you can access the database directly with $wpdb. At the higher layer, you can often use WordPress functions that are easier to read, more standard, and closer to how plugin and theme code is usually written.

So what we have to bear in mind is:

  • $wpdb = direct database access
  • get_option() = normal WordPress way to read one option
  • get_userdata() = normal WordPress way to read one user
  • get_posts() = normal WordPress way to read a list of posts

You should also notice that these functions return different shapes of data depending on what they are meant to fetch. That is normal. Right now, we should just start getting comfortable reading real data through WordPress itself, not only through SQL.

References: