How to display the posts list in WordPress?

We are going to learn how to display the posts list in WordPress without any plugin. We will use the simplest way and simple PHP code. To insert the PHP code in WordPress post, we’ll use PHP code snippets (Insert PHP) plugin.
So, open or add a new page/post and click on the ‘Text’ tab to write the code. After activated the PHP code snippets plugin, we can use now.

insert_php

MySQL connection

Now, we need to create the connection to access the MySQL database where WordPress database is created. Let’s have a look at the PHP code.

$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);

What is $conn? it recognizes the “conn” as a variable …OK, and what does the mean of “new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);”? mysqli is a PHP class to connect MySQL. Where are we initialized the DB_HOST, DB_USER, DB_PASSWORD and DB_NAME variables?… hmm, good question… look at this

if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
Actually, MySQL settings are saved in wp-config.php and above code, use retrieve the settings from wp-config.php file.

like /** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define(‘DB_USER’, ‘wordpressuser’);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

Above code is used to check whether connection failed or not. OK?… After connecting to the database we need to write a query.

$sql = "SELECT post_title,ID, DATE_FORMAT(post_date_gmt,'%Y/%m/%d') as post_date_gmt,post_name FROM wp_posts where post_type='post' and post_status='publish' order by post_date desc";

wp_posts” is table name where all posts are saved.
“DATE_FORMAT(post_date_gmt,’%Y/%m/%d’) as post_date_gmt” used to create the hyperlink.

$result = $conn->query($sql);

Above code used to retrieve the data by given connection. Now, it’s time to display the list of posts which post
_type=’post’ and post_staus is ‘publish’.

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<li>";
echo "<a href='".$row["post_date_gmt"]."/".$row["post_name"]."' >".$row["post_title"]." </a> ";
echo "</li>";
}
} else {
echo "0 results";
}

After save or publish, we can see the list of posts WOW!

Here is complete code to display the WordPress posts list.
* Please don’t forget to add the code inside

insert_php

// Create connection
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);

// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
$sql = “SELECT post_title,ID, DATE_FORMAT(post_date_gmt,’%Y/%m/%d’) as post_date_gmt,post_name FROM wp_posts where post_type=’post’ and post_status=’publish’ order by post_date desc”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo “<li>”;
echo “<a href='”.$row[“post_date_gmt”].”/”.$row[“post_name”].”‘ >”.$row[“post_title”].” </a> “;
echo “</li>”;
}
} else {
echo “0 results”;
}
$conn->close();

I hope we have learned How to display the posts list in WordPress.

Thanks for reading my blog. Read my other blogs.