Skip to content

How to Create a Basic WordPress Plugin to List All the Posts?

Wordpress-How-to-display-all-posts-in-WordPress

Overview:

In this post, I’ll go through PHP code creating a simple WordPress plugin to display a list of your WordPress website posts and include different details of the post in the list.

Create a basic Plugin for the WordPress:

For this purpose I’ll be creating a simple plugin. To create a plugin you simply need to add the following code to the index.php file of the plugin:

/**
 * Plugin Name: Plugin Name
 * Plugin URI: https://codingarc.com
 * Description: This plugin exports Posts.
 * Version: 1.0
 * Author: CodingArc
 * Author URI: https://codingarc.com
 */

Add an Admin Sidebar Menu Item for the Worpdress plugin:

Next, I’ll add a menu tab in the WordPress Dashboard sidebar. The following code will add the Menu item ‘Export Posts’ to the sidebar.

add_action('admin_menu', 'export_posts_plugin_setup_menu');
 
function export_posts_plugin_setup_menu(){
        add_menu_page( 'All In Export Posts Plugin Page', 'Export Posts', 'manage_options', 
                       'all-in-export-posts-plugin', 'export_posts_init' );
}
 
function export_posts_init(){
        echo "<h1>Export Posts:</h1>";
	// TODO: call the function to get the wordpress posts and display
}

Now comes the main part of the code where we fetch the posts from our WordPress database and display the relevant data. For this purpose, I’m listing here two approaches, each with a separate function below. You will need to call the name of function() in the export_posts_init function.

Fetch posts from the WordPress feed:

This code will use your website feed, which you can define in $feed variable at ‘yourwebsite/feed/’. The $max_posts_to_show is the number of posts to show per page, but if your WordPress settings is set to a lower number of posts per page then the lower value is used.

function export_posts_from_feed() {
	$feed = 'https://codingarc.com/feed/';
	$max_posts_to_show = 50;
	$rss = new DOMDocument();
	$rss->load($feed);
	$feed = array();
	echo 'Exporting '.$max_posts_to_show.' posts using the feed: '.$feed.'<br/><br/>';
	foreach ($rss->getElementsByTagName('item') as $node) {
		$item = array ( 
			'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
			'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
			'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
			'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
			'cat' => $node->getElementsByTagName('category')->item(0)->nodeValue,);
        	array_push($feed, $item);
    	}
    	$limit = $max_posts_to_show;
    	for($x=0;$x<$limit;$x++) {
        	$title = str_replace(' & ', ' & ', $feed[$x]['title']);
        	$link = $feed[$x]['link'];
        	$description = $feed[$x]['desc'];
        	$date = date('l F d, Y'.'', strtotime($feed[$x]['date']));
        	$category = $feed[$x]['cat'];
        	echo $link.', '.$category.', link: <a href="'.$link.'"title="'.
                     $title.'" target="_blank">'.$title.'</a> <br/><br/>';
	}
}

Retrieve posts from the WordPress loop:

This will retrieve all posts that are published. You can change the status from ‘post_status’ and choose ‘DESC’ or ‘ASC’ in the order, also the ‘post_type’. The ‘posts_per_page’ determines how many posts to show per page and if it’s set to -1 it will retrieve all posts.

function export_posts_from_wp() {
	$args = array(
	'post_type'=> 'post',
	'orderby'    => 'ID',
	'post_status' => 'publish',
	'order'    => 'DESC',
	'posts_per_page' => -1 // this will retrieve all the post that is published 
	);
	$result = new WP_Query( $args );
	if ( $result-> have_posts() ) : 	
		while ( $result->have_posts() ) : $result->the_post();
			echo  get_permalink() . '<br/> ';
			echo  get_the_ID() . '<br/> ';
			echo  get_the_category() . '<br/>';
			echo  the_title() . '<br/><br/>';
		endwhile;
	endif; 
	wp_reset_postdata();
}

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.