Learn WordPress Built-in Functions & Custom Queries

Reading Time: 10 minutes
3,169 Views

In this article we will use the WordPress Built-in Functions & Custom Queries like to get all wordpress posts, all users etc. Also we will see how can we use custom queries using wordpress global object $wpdb.

WordPress built-in functions are pre-defined functions that are included in the WordPress core files and can be used in themes, plugins, or custom code.

Learn More –

Let’s get started.


How can we create a Basic Widget Plugin – WordPress Development

Widget is the extra information section in any wordpress site. Widgets are generally provided by wordpress theme. Basics of wordpress widget, click here.

Any widget in wordpress can be developed in a very simple and easy steps. In widget development, WP_Widget class plays an important role. This is the parent class which we extends into custom widget class. More information about WP_Widget class on a different article. Widget class contains these following methods __construct(), form(), update() and widget().

Complete details about __construct() & from() methods available here – Click here. After these methods we have now for update() method & widget() method. As, I already discussed several times inside this course articles about above methods in detail. In the previous article we have wrapped each thing step by step and developed a basic widget plugin with few input fields like – title and widget description.


What are wordpress in-built functions ?

WordPress functions can be defined in several categories. Categories I mean to say like wordpress theme functions, wordpress plugin functions, widget functions, helper functions, methods called by an object like inside WP_Widget class etc. WordPress available functions makes the wordpress working behaviour more perfect. Sometimes development seems to be easy.

Let’s take few examples of wordpress in-built functions.

  1. get_posts() – To get all posts from wodpress database.
  2. get_users() – List all wordpress users.
  3. get_categories() – List all wordpress categories in object format.

There are only 3 to understand else, Rest we have too many function in wordpress.

We have taken few functions here. These functions provides or completes some specific task. Also if we want some filtered result from these functions like – need only post_type = “post” not any page, so according to the condition we can get only posts. Additionally, if we want only author user type, then we can pass role in to get_users function.

// Code block to get all pages
$all_pages = get_posts(array(
	"post_type" => "page"
));
// print_r($all_pages);
// Code block to get all authors
$all_authors = get_users(array(
  'role__in' => [ 'author' ] 
));
// print_r($all_authors);

What is WordPress Global Object $wpdb ?

WordPress is powerful CMS in PHP. Now a days, it is one of most popular platform where sites are regularly launching. All working of wordpress can be easily manged by it’s plugins, widgets etc. It can be easily managed by any users either they are technical, no technical or even they are developer. All have their point of interest of working here.

For technical person they can manage database, changes via admin panel etc. Non technical person also have the easy and handy UI of admin and site where by simply drag and drop they can manage and work. Every time they don’t actually need some worker to work on it. Developer has it’s own field of development like widget, theme, plugin etc.

WordPress provides many of in-built functions which already discussed previously before this topic, global object which we can any where in the wordpress development.

We have one of the global object called $wpdb. It is very important to use in wordpress development. wp-db.php is the class file from where $wpdb instance came. wp-db.php file is in /wp-includes folder. We can also, if want our own custom variable we can create and use it of Wpdb class. For more information about $wpdb click here.

Already we have seen the following methods & how can we use

Please follow the given links and get more idea about each given topic in detailed concept.


Where we will use the wordpress functions in Widget Development ?

As we already discussed that WP_Widget class provides 18 different different methods as per wordpress official documentation. Each method we can use and do widget development.

Most important methods to create a basic widget already seen as – __construct(), form(), update() & widget().

WordPress functions we can any of these specified methods. widget() method for output the settings and values where as form() method is for the layout and input settings at admin panel.

If we want to use wordpress function at admin level to create drop down values of users, we can use and create.

Here, we have list of wordpress users,

// form method - display list users with other fields at admin panel

public function form($instance)
    {

        $all_users = get_users();

        // Used for creating admin panel layouts of widget
        $title = !empty($instance['title']) ? $instance['title'] : "";
        $description = !empty($instance['description']) ? $instance['description'] : "";
        $user = !empty($instance['user']) ? $instance['user'] : "";
        ?>
		<p>
			<label for="<?php echo $this->get_field_id('txt_title') ?>">Widget Title</label>
			<input type="text" name="<?php echo $this->get_field_name('txt_title') ?>" id="<?php echo $this->get_field_id('txt_title') ?>" placeholder="Enter Widget Title" value="<?php echo $title; ?>" class="widefat" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id('description') ?>">Description</label>
			<textarea class="widefat" id="<?php echo $this->get_field_id('description') ?>" name="<?php echo $this->get_field_name('description') ?>" placeholder="Description" ><?php echo $description; ?></textarea>
		</p>

        <p>
            <label for="<?php echo $this->get_field_id('user') ?>">Users</label>
            <select class="widefat" id="slct-users" name="users">
                <option value="">Select User</option>
                <?php
                  if(count($all_users) > 0){
                    foreach($all_users as $key => $value) {
                        
                        $user = $value->data;
                        ?>
                        <option value="<?php echo $user->ID ?>"><?php echo ucwords($user->display_name); ?></option>
                        <?php
                    }
                  }
                ?>
            </select>
        </p>
		<?php
}

It Outputs


Add Custom Queries to Widget Method

Here, we place only custom query of $wpdb instead of using wordpress function.

public function form($instance)
{
        global $wpdb;

        $all_users = $wpdb->get_results(
            "SELECT ID, name from ".$wpdb->prefix."users"
        );
  
    // REST CODE OF WIDGET
}

I hope you got the idea now about wordpress functions, custom queries, how to use in widget development etc.


Article QA – Interview Questions

Here, we will see few important questions from this article. These questions are important for interview.

  • What are important methods in WP_Widget ?
  • Can you please write the syntax to create widget ?
  • Write few examples of wordpress function.
  • Do you know what is global wordpress object ?
  • List few methods which we call via $wpdb object.
  • How we use get_posts() function in widget development ?
  • Please write a query to get all posts from wordpress database excluding post IDs 2, 3, 5.

We hope this article helped you to learn about using WordPress Built functions in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.