DB

What is the difference between posts_per_page and numberposts in WordPress
What is the difference between posts_per_page and numberposts in WordPress

This post was written both for myself and for anyone who's ever wondered — what's the difference between the “numberposts” and “posts_per_page” parameters passed to the get_posts function?

I sometimes find myself trying to recall the difference, and in various places I use one or the other.

read more...

How to display posts sorted by latest comments in WordPress
How to display posts sorted by latest comments in WordPress

WordPress (WP) has a universal function called `get_posts`, but unfortunately, it doesn’t work in 100% of cases. Sometimes, to get the desired result, you need to manually build an SQL query. I don’t really like doing that, since I don’t know all the ins and outs of WP’s internal mechanisms (such as where and when certain filters should be applied). But when the task requires it, you have to write custom SQL.

read more...

How to establish a connection to a database in the Yii2 framework
How to establish a connection to a database in the Yii2 framework

A simple and trivial task that a beginner Yii2 developer may encounter. Let's start with the fact that depending on the project configuration, the database can be set up in different ways. Let’s consider the two options provided by the framework:

  1. basic (simple setup)
  2. advanced (more complex setup)

In both cases, the database configuration (setting the prefix/tablePrefix, username/username, password/password, dsn, etc.) is done the same way. It all depends on the file location and the database connection class. In this example, we’ll look at how to connect to a MySQL database, as it's one of the most popular options.

In the basic template, the database configuration file can be found at: “/config/db.php”.

In the advanced template, things are a bit more complex (hence the name — for large-scale projects). There may be more than one configuration file (for backend, frontend, etc.). It all depends on how you initially structured your application. Since both admin and frontend parts usually work with the same database, I typically place the database configuration in the “/common/config/db.php” directory. Then I reference it in the “/common/config/main.php” config. That way, both backend and frontend will automatically inherit the database settings.

read more...

How to Create a Table in WordPress Database
How to Create a Table in WordPress Database

Creating a custom table for the WordPress CMS may be necessary in the following cases:

  1. When developing your own plugin
  2. When creating your own theme (e.g., for real estate, car sales or rental, etc.)

In my projects, to create tables or import data into the database, I use the WP function `dbDelta` within the `register_activation_hook()`. Example code:

register_activation_hook(__FILE__, function()
{
	global $wpdb;
	
	require_once(ABSPATH.'wp-admin/includes/upgrade.php');

	dbDelta("CREATE TABLE IF NOT EXISTS `{$wpdb -> prefix}my_table` (
		`id` INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
		`title` VARCHAR(255) NOT NULL,
		`date_create` INT(10) UNSIGNED NOT NULL
	) {$wpdb -> get_charset_collate()};");	
});

read more...