<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amit Biswas</title>
    <description>The latest articles on DEV Community by Amit Biswas (@amitbiswasta).</description>
    <link>https://dev.to/amitbiswasta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F372687%2F273f95b9-a9e4-4ada-90d8-b653687086a2.jpg</url>
      <title>DEV Community: Amit Biswas</title>
      <link>https://dev.to/amitbiswasta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amitbiswasta"/>
    <language>en</language>
    <item>
      <title>Custom post type capabilities and roles in WordPress</title>
      <dc:creator>Amit Biswas</dc:creator>
      <pubDate>Tue, 08 Sep 2020 11:04:17 +0000</pubDate>
      <link>https://dev.to/amitbiswasta/custom-post-type-capabilities-and-roles-in-wordpress-53pi</link>
      <guid>https://dev.to/amitbiswasta/custom-post-type-capabilities-and-roles-in-wordpress-53pi</guid>
      <description>&lt;p&gt;We often use custom post types for various purposes. I believe all WordPress plugin developers love this feature. But many of us who are relatively new in WordPress development, probably not aware of custom capabilities and it's potentials. In this article, I will try to put some light on custom post type capabilities.&lt;/p&gt;

&lt;h2&gt;Why use new capabilities for custom post type&lt;/h2&gt;

&lt;p&gt;The heart of the question is why I should use new capabilities instead of the defaults. The answer is simple. Whenever you need precise control for different users, you need custom capabilities. For example, if your plugin have custom post types and you want to distribute specific tasks to different users, you need custom capabilities.&lt;/p&gt;

&lt;p&gt;Let's say I have a post type &lt;code&gt;book&lt;/code&gt; and only authors can create, edit books. I will assign custom capabilities to authors only. There is a brilliant plugin available to customize user roles and capabilities. It's called "&lt;a href="https://wordpress.org/plugins/user-role-editor/" rel="noopener noreferrer"&gt;User Role Editor&lt;/a&gt;". By default, WordPress assigns capabilities based on the user roles such as "admin", "editor", "author" and so on. To know about user role based capabilities, use &lt;code&gt;&lt;a href="https://developer.wordpress.org/reference/functions/get_role/" rel="noopener noreferrer"&gt;get_role()&lt;/a&gt;&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;How to create new capabilities&lt;/h3&gt;

&lt;p&gt;Now that we have understood the potentials, let's see how we can create. When we register a new post type in a plugin, we can define custom capabilities. &lt;/p&gt;

&lt;p&gt;A custom post type needs to be registered with &lt;code&gt;register_post_type()&lt;/code&gt; function and &lt;code&gt;init&lt;/code&gt; action hook. For example, let's register a &lt;code&gt;book&lt;/code&gt; post type with &lt;strong&gt;custom capabilities&lt;/strong&gt; -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function book_post_type() {
    $args = array(
        'public'          =&amp;gt; true,
        'label'           =&amp;gt; __( 'Books', 'textdomain' ),
        'menu_icon'       =&amp;gt; 'dashicons-book',
        'show_in_rest'    =&amp;gt; true,
        'taxonomies'      =&amp;gt; array( 'category' ),
        'capability_type' =&amp;gt; array('book','books'), //custom capability type
        'map_meta_cap'    =&amp;gt; true, //map_meta_cap must be true
    );
    register_post_type( 'book', $args );
}
add_action( 'init', 'book_post_type' ); //action hook&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you notice, we have used &lt;code&gt;'capability_type' =&amp;gt; array('book','books')&lt;/code&gt; which will create new capability type for the &lt;code&gt;book&lt;/code&gt; post type. It is also important to use &lt;code&gt;'map_meta_cap' =&amp;gt; true,&lt;/code&gt; for custom post types. If you don't set it &lt;code&gt;true&lt;/code&gt;, WordPress will not be able to map new capabilities. Default capability keys are -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/**
 * Default capability type keys
 * Will be mapped with our types which are "book"(singular), "books"(plural)
 */

[edit_post]              =&amp;gt; edit_book
[read_post]              =&amp;gt; read_book
[delete_post]            =&amp;gt; delete_book
[edit_posts]             =&amp;gt; edit_books
[edit_others_posts]      =&amp;gt; edit_other_books
[publish_posts]          =&amp;gt; publish_books
[read_private_posts]     =&amp;gt; read_private_books
[delete_posts]           =&amp;gt; delete_books
[delete_private_posts]   =&amp;gt; delete_private_books
[delete_published_posts] =&amp;gt; delete_published_books
[delete_others_posts]    =&amp;gt; delete_others_books
[edit_private_posts]     =&amp;gt; edit_private_books
[edit_published_posts]   =&amp;gt; edit_published_books&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;The restrictions&lt;/h3&gt;

&lt;p&gt;Now at this point, WordPress will not show the custom post type (book) for any logged in user. This is for security purposes. As we have added new capabilities, we need to tell WordPress which user have these capabilities.&lt;/p&gt;

&lt;h2&gt;Add newly added capabilities for admin&lt;/h2&gt;

&lt;p&gt;Admin users are at the top level with all default capabilities assigned to them. So admin users also should have all custom capabilities. Let's do that in an efficient way.&lt;/p&gt;

&lt;p&gt;Role based capability can be added by the &lt;code&gt;&lt;a href="https://developer.wordpress.org/reference/classes/wp_role/add_cap/" rel="noopener noreferrer"&gt;WP_Role::add_cap( &lt;em&gt;string&lt;/em&gt; $cap )&lt;/a&gt;&lt;/code&gt; function. This is important that we hook this function on plugin or theme activation. We shall use a class based static methods for better code management. We need to add the following code to the main plugin file.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// add custom capabilities to the admin on plugin activation
register_activation_hook( __FILE__, array( 'Admin_Roles', 'add_admin_capabilities' ) );&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;The main class for admin role&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;/**
 * Main class for ADMINISTRATOR
 */
class Admin_Roles {
    /**
     * Custom capabilities of custom post types
     */
    private static $customCaps = array(
        [ 'singular' =&amp;gt; 'book', 'plural' =&amp;gt; 'books' ],
    );

    /**
     * Add custom capabilities for admin
     */
    public static function add_admin_capabilities() {

        $role = get_role( 'administrator' );

        foreach( self::$customCaps as $cap ){
            
            $singular = $cap['singular'];
            $plural = $cap['plural'];

            $role-&amp;gt;add_cap( "edit_{$singular}" ); 
            $role-&amp;gt;add_cap( "edit_{$plural}" ); 
            $role-&amp;gt;add_cap( "edit_others_{$plural}" ); 
            $role-&amp;gt;add_cap( "publish_{$plural}" ); 
            $role-&amp;gt;add_cap( "read_{$singular}" ); 
            $role-&amp;gt;add_cap( "read_private_{$plural}" ); 
            $role-&amp;gt;add_cap( "delete_{$singular}" ); 
            $role-&amp;gt;add_cap( "delete_{$plural}" );
            $role-&amp;gt;add_cap( "delete_private_{$plural}" );
            $role-&amp;gt;add_cap( "delete_others_{$plural}" );
            $role-&amp;gt;add_cap( "edit_published_{$plural}" );
            $role-&amp;gt;add_cap( "edit_private_{$plural}" );
            $role-&amp;gt;add_cap( "delete_published_{$plural}" );
            
        }

    }

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now that we have our class ready, we can add any number of post type capabilities for admin. We just need to add capability types to &lt;code&gt;private static $customCaps&lt;/code&gt; variable. For example, a newly added custom post type capability can be added like this -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;private static $customCaps = array(
    [ 'singular' =&amp;gt; 'book', 'plural' =&amp;gt; 'books' ],
    [ 'singular' =&amp;gt; 'story', 'plural' =&amp;gt; 'stories' ],
);
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Deactivate and reactivate the plugin&lt;/h3&gt;

&lt;p&gt;As we have used &lt;code&gt;register_activation_hook()&lt;/code&gt; function, you need to deactivate and then reactivate the plugin to make this code work. The activation hook will add all the new capabilities for the admin. From this point, you may use the "&lt;a href="https://wordpress.org/plugins/user-role-editor/" rel="noopener noreferrer"&gt;User Role Editor&lt;/a&gt;" plugin and control user roles and assign custom post type capabilities for them.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;All custom post types should have custom capability types as it gives the power to the plugin users. This particular method is very simple and manageable. I use this method for all of my plugins that registers custom post types.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed this article and if you have any question, please ask in the comment section.&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>php</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Philosophy Quiz - A JavaScript Project</title>
      <dc:creator>Amit Biswas</dc:creator>
      <pubDate>Wed, 08 Jul 2020 09:54:55 +0000</pubDate>
      <link>https://dev.to/amitbiswasta/philosophy-quiz-a-javascript-project-1d6f</link>
      <guid>https://dev.to/amitbiswasta/philosophy-quiz-a-javascript-project-1d6f</guid>
      <description>&lt;p&gt;This is an example project of JavaScript. This project creates and evaluates results of a quiz which is purely object based. All the JavaScript code is well documented and commented for better understanding of practical JS use.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amitbiswas06/embed/abdqeOx?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Cover image by &lt;a href="https://pixabay.com/users/cdd20-1193381/" rel="noopener noreferrer"&gt;Cdd20&lt;/a&gt; from Pixabay&lt;/p&gt;

</description>
      <category>codepen</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Get categories by post type in WordPress</title>
      <dc:creator>Amit Biswas</dc:creator>
      <pubDate>Fri, 22 May 2020 11:35:15 +0000</pubDate>
      <link>https://dev.to/amitbiswasta/get-categories-by-post-type-in-wordpress-d8b</link>
      <guid>https://dev.to/amitbiswasta/get-categories-by-post-type-in-wordpress-d8b</guid>
      <description>&lt;p&gt;Have you ever wondered about how to get categories by post type? Categories is the default taxonomy for WordPress just like the tags. But categories have hierarchies and tags don't have. Categories are used for the posts by default. However, if we register a new custom post type for which we wish to add categories, it's fairly simple job to do.&lt;/p&gt;

&lt;p&gt;Registering a new post type is a plugin functionality and we should always do it by creating a custom plugin. You may find the developers documentation for &lt;a href="https://developer.wordpress.org/reference/functions/register_post_type/" rel="noopener noreferrer"&gt;registering a custom post type&lt;/a&gt; here.&lt;/p&gt;

&lt;h3&gt;Practical scenario&lt;/h3&gt;

&lt;p&gt;Let's say we have a custom post type "&lt;em&gt;movies&lt;/em&gt;" which also shares the category with the posts. Now, we have categories "&lt;strong&gt;comedy&lt;/strong&gt;", "&lt;strong&gt;horror&lt;/strong&gt;" and "&lt;strong&gt;adventure&lt;/strong&gt;" for the &lt;em&gt;&lt;span&gt;movies&lt;/span&gt;&lt;/em&gt; posts and "&lt;strong&gt;stories&lt;/strong&gt;", "&lt;strong&gt;interviews&lt;/strong&gt;", "&lt;strong&gt;reviews&lt;/strong&gt;" for the default &lt;em&gt;&lt;span&gt;post&lt;/span&gt;&lt;/em&gt;s. So, for both of the archives we want to show only those categories that relates to the post type itself.&lt;/p&gt;

&lt;h3&gt;The issues&lt;/h3&gt;

&lt;p&gt;There are several ways to get categories in WordPress. The &lt;code&gt;get_categories()&lt;/code&gt; function will retrieve all the categories available in your site regardless of post type. Similarly &lt;code&gt;wp_list_categories()&lt;/code&gt; and &lt;code&gt;get_terms()&lt;/code&gt; can also fetch and display categories or terms. But none of them have the option to retrieve categories for a given post type.&lt;/p&gt;

&lt;h3&gt;Our goal to achieve&lt;/h3&gt;

&lt;p&gt;In this tutorial we shall discover how we can get categories by post type. In our test case, the movies posts archive will show only "&lt;strong&gt;comedy&lt;/strong&gt;", "&lt;strong&gt;horror&lt;/strong&gt;" and "&lt;strong&gt;adventure&lt;/strong&gt;" and the default posts archive will show "&lt;strong&gt;stories&lt;/strong&gt;", "&lt;strong&gt;interviews&lt;/strong&gt;", "&lt;strong&gt;reviews&lt;/strong&gt;". Let's see how we shall do it.&lt;/p&gt;

&lt;h3&gt;Coding plans&lt;/h3&gt;

&lt;p&gt;To achieve this, we have to think in a different way. WordPress functions covers everything that you need to do in a development. We shall explore some of the functions for this.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;get_posts()&lt;/code&gt; function retrieve posts array which includes &lt;strong&gt;post ID&lt;/strong&gt;. The &lt;code&gt;get_the_terms()&lt;/code&gt; function retrieves the terms of the taxonomy that are attached to the post. Now, we have something to start with. Our approach will be to make a function which will do -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Get all posts with &lt;code&gt;get_posts()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Then loop through the posts array and retrieve the terms attached to those posts with &lt;code&gt;get_the_terms()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Store the new terms objects within a new array.&lt;/li&gt;
&lt;li&gt;Use the new array and loop through to make a new array of terms with our desired &lt;strong&gt;key&lt;/strong&gt;, &lt;strong&gt;value&lt;/strong&gt; pair.&lt;/li&gt;
&lt;li&gt;Finally, make that array unique as duplicate entries can be there.&lt;/li&gt;
&lt;li&gt;Return the final array.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;The code snippet to get categories by post type&lt;/h2&gt;

&lt;p&gt;Here is the full snippet that will retrieve categories or custom taxonomy terms by post type. You may place it in your theme's function.php.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;if( !function_exists('tartist__get_terms_by_post_type') ){

    function tartist__get_terms_by_post_type( $postType = 'post', $taxonomy = 'category'){

        /**
         * Get Terms by post type
         * @author Amit Biswas
         * 
         * @param postType default 'post'
         * @param taxonomy default 'category'
         * 
         * @return array of terms for a given $posttype and $taxonomy
         * @since 1.0.1
         * 
         * 1. Get all posts by post type
         * 2. Loop through the posts array and retrieve the terms attached to those posts
         * 3. Store the new terms objects within `$post_terms`
         * 4. loop through `$post_terms` as it's a array of term objects.
         * 5. store the terms with our desired key, value pair inside `$post_terms_array`
         */

        //1. Get all posts by post type
        $get_all_posts = get_posts( array(
            'post_type'     =&amp;gt; esc_attr( $postType ),
            'post_status'   =&amp;gt; 'publish',
            'numberposts'   =&amp;gt; -1
        ) );

        if( !empty( $get_all_posts ) ){

            //First Empty Array to store the terms
            $post_terms = array();
            
            //2. Loop through the posts array and retrieve the terms attached to those posts
            foreach( $get_all_posts as $all_posts ){

                /**
                 * 3. Store the new terms objects within `$post_terms`
                 */
                $post_terms[] = get_the_terms( $all_posts-&amp;gt;ID, esc_attr( $taxonomy ) );

            }

            //Second Empty Array to store final term data in key, value pair
            $post_terms_array = array();

            /**
             * 4. loop through `$post_terms` as it's a array of term objects.
             */

            foreach($post_terms as $new_arr){
                foreach($new_arr as $arr){

                    /**
                     * 5. store the terms with our desired key, value pair inside `$post_terms_array`
                     */
                    $post_terms_array[] = array(
                        'name'      =&amp;gt; $arr-&amp;gt;name,
                        'term_id'   =&amp;gt; $arr-&amp;gt;term_id,
                        'slug'      =&amp;gt; $arr-&amp;gt;slug,
                        'url'       =&amp;gt; get_term_link( $arr-&amp;gt;term_id )
                    );
                }
            }

            //6. Make that array unique as duplicate entries can be there
            $terms = array_unique($post_terms_array, SORT_REGULAR);

            //7. Return the final array
            return $terms;

        }

    }

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The function &lt;code&gt;tartist__get_terms_by_post_type()&lt;/code&gt; accepts 2 parameters. First parameter is for the post type, second parameter is for taxonomy. Both parameters are optional as the default value is set to &lt;em&gt;post&lt;/em&gt; and &lt;em&gt;category&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;Return value&lt;/h3&gt;

&lt;p&gt;On success, this function returns the terms array for the given post type and taxonomy. Here is an example -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;//store the categories for movies post type
$postTerms = tartist__get_terms_by_post_type('movies');
var_dump( $postTerms );&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above code will return the categories for &lt;em&gt;movies&lt;/em&gt;. As per our example -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;array (size=3)
    0 =&amp;gt; 
        array (size=4)
        'name'    =&amp;gt; string 'Comedy' (length=6)
        'term_id' =&amp;gt; int 7
        'slug'    =&amp;gt; string 'comedy' (length=6)
        'url'     =&amp;gt; string 'http://example.com/category/comedy/' (length=35)
    1 =&amp;gt; 
        array (size=4)
        'name'    =&amp;gt; string 'Horror' (length=6)
        'term_id' =&amp;gt; int 9
        'slug'    =&amp;gt; string 'horror' (length=6)
        'url'     =&amp;gt; string 'http://example.com/category/horror/' (length=35)
    2 =&amp;gt; 
        array (size=4)
        'name'    =&amp;gt; string 'Adventure' (length=9)
        'term_id' =&amp;gt; int 10
        'slug'    =&amp;gt; string 'adventure' (length=9)
        'url'     =&amp;gt; string 'http://example.com/category/adventure/' (length=38)&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;How to use&lt;/h3&gt;

&lt;p&gt;There are many scopes where you can use this function. For instance, you can create a new widget that shows the categories by post type. Or you may use this to create interactive forms etc. It's totally up to you, how you will use this in your project.&lt;/p&gt;

&lt;p&gt;For example, let's create an unordered list of terms with this - &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;ul&amp;gt;
&amp;lt;?php
    //store the categories for movies post type
    $postTerms = tartist__get_terms_by_post_type('movies');
    
    if( !empty( $postTerms ) ){
        foreach( $postTerms as $term ){ 
?&amp;gt;
    &amp;lt;li&amp;gt;&amp;lt;a href="&amp;lt;?php echo esc_url( $term['url'] ); ?&amp;gt;&amp;gt;&amp;lt;?php echo esc_html( $term['name'] ); ?&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;?php } } ?&amp;gt;
&amp;lt;/ul&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Special note&lt;/h3&gt;

&lt;p&gt;If you check our main function, at point 5 where we are making the &lt;code&gt;$post_terms_array&lt;/code&gt; array, you can add anything that is relevant to terms. Here we have added the term archive url parameter which is not originally returned by the &lt;code&gt;get_the_terms()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed the tutorial and if you find it helpful, I will appreciate if you share the article with others too. Thank you all.&lt;/p&gt;

&lt;p&gt;Featured image by &lt;a href="https://unsplash.com/@martinshreder?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Martin Shreder&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/technology?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>tutorial</category>
      <category>php</category>
      <category>snippet</category>
    </item>
    <item>
      <title>How to create a WordPress posts archive with REST API and ajax - A practical example</title>
      <dc:creator>Amit Biswas</dc:creator>
      <pubDate>Thu, 14 May 2020 11:14:05 +0000</pubDate>
      <link>https://dev.to/amitbiswasta/how-to-create-a-wordpress-posts-archive-with-rest-api-and-ajax-a-practical-example-1a25</link>
      <guid>https://dev.to/amitbiswasta/how-to-create-a-wordpress-posts-archive-with-rest-api-and-ajax-a-practical-example-1a25</guid>
      <description>&lt;p&gt;In this tutorial, we shall learn about a different approach of making posts archive in a WordPress site. Instead of the default WordPress loop or &lt;code&gt;WP_Query()&lt;/code&gt;, we will be using WordPress REST API and jQuery Ajax. Here's the codepen that we shall try to achieve.
You can find the full tutorial here - &lt;a href="https://templateartist.com/2020/05/12/wordpress-custom-posts-archive-with-rest-api-and-ajax/" rel="noopener noreferrer"&gt;WordPress posts archive with REST API and ajax.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amitbiswas06/embed/PoPavyZ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>tutorial</category>
      <category>javascript</category>
      <category>codepen</category>
    </item>
    <item>
      <title>Retrieve WordPress posts array with ID and title</title>
      <dc:creator>Amit Biswas</dc:creator>
      <pubDate>Mon, 04 May 2020 07:21:50 +0000</pubDate>
      <link>https://dev.to/amitbiswasta/retrieve-wordpress-posts-array-with-id-and-title-3e4i</link>
      <guid>https://dev.to/amitbiswasta/retrieve-wordpress-posts-array-with-id-and-title-3e4i</guid>
      <description>&lt;h3&gt;Introduction&lt;/h3&gt;

&lt;p&gt;As a WordPress developer, I often feel the need for post title and post ID together. It seems easy task using the available WordPress function like &lt;code&gt;get_the_title()&lt;/code&gt;. But outside the loop that only works if you know the post ID.  However, what if we want to retrieve all posts title with ID? Or posts by category etc?&lt;/p&gt;

&lt;p&gt;I will show you a powerful way to do that and store our data inside an array with &lt;code&gt;$key=&amp;gt;$value&lt;/code&gt; settings and for this I will use the native WordPress function called "&lt;code&gt;get_posts()&lt;/code&gt;".&lt;/p&gt;

&lt;h3&gt;Where to write&lt;/h3&gt;

&lt;p&gt;Any code snippet can be written directly in your theme's &lt;strong&gt;"functions.php"&lt;/strong&gt; file or if you are writing a plugin, you may place it inside your main plugin file. Though if you have many snippets for your project, you may place the code inside a separate PHP file and then you can call the file inside the functions.php using the &lt;code&gt;require()&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;Code snippet&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;/**
 * Retrieve posts
 * @param post_type
 * (string|array) A post type slug (string) or array of post type slugs. Default 'post'.
 * @param total_posts
 * (int) The number of posts to query for. Use -1 to request all posts. Default 10.
 * @param order
 * (string) Designates ascending or descending order of posts. 
 * Default 'DESC'. Accepts 'ASC', 'DESC'.
 * 
 * @return array( key =&amp;gt; value )
 */
function tartist__posts_array( $post_type = '', $total_posts = '', $order = '' ){
    
    $get_all_posts = get_posts( array(
        'post_type'     =&amp;gt; $post_type ? esc_attr( $post_type ) : 'post',
        'post_status'   =&amp;gt; 'publish',
        'numberposts'   =&amp;gt; $total_posts ? esc_attr( $total_posts ) : 10,
        'order'     =&amp;gt; $order ? esc_attr( $order ) : 'DESC'
    ) );

    $posts = array();

    foreach( $get_all_posts as $newPosts ){
        $posts[$newPosts-&amp;gt;ID] = esc_html($newPosts-&amp;gt;post_title);
    }

    return $posts;

}&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;How to use&lt;/h3&gt;

&lt;p&gt;As you may see, the function &lt;code&gt;tartist__posts_array()&lt;/code&gt; accepts 3 parameters and those are optional. If no parameters are given, this function will return the array with default settings. You can call this function in any WordPress template or function. For example, if you paste this code inside your single.php as &lt;code&gt;var_dump( tartist__posts_array() );&lt;/code&gt; it will show you the returned array just like the example below -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;array (size=3)
  1 =&amp;gt; string 'Title 1' (length=7)
  2 =&amp;gt; string 'Title 2' (length=7)
  3 =&amp;gt; string 'Title 3' (length=7)
 
  ... and so on&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, you can easily use this array to extract the values and titles as per your need. You can also use this function to return a custom post type posts. The first parameter will handle that. The second parameter is for total number of posts you want to return and the last parameter is for ordering. I will show you a practical example now.&lt;/p&gt;

&lt;h3&gt;A practical example of use&lt;/h3&gt;

&lt;p&gt;Suppose if you need a dynamic drop down select for a form, you can easily do so by using the array like the example below -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$postsArray = tartist__posts_array();

&amp;lt;select&amp;gt;
&amp;lt;?php foreach( $postsArray as $key=&amp;gt;$value ){ ?&amp;gt;
    &amp;lt;option value="&amp;lt;?php echo esc_attr( $key ); ?&amp;gt;" &amp;gt;&amp;lt;?php echo esc_html( $value ); ?&amp;gt;&amp;lt;/option&amp;gt;
&amp;lt;?php } ?&amp;gt;
&amp;lt;/select&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above code will render a drop down select with options in HTML format like this -&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;select&amp;gt;        
    &amp;lt;option value="1"&amp;gt;Title 1&amp;lt;/option&amp;gt;
    &amp;lt;option value="2"&amp;gt;Title 2&amp;lt;/option&amp;gt;
    &amp;lt;option value="3"&amp;gt;Title 3&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Apart from this, there can be many more usages as you can imagine. It will save a lot of time for your project if you use it in proper place with proper ideas. I have used similar function in one of me free Elementor extension. You can download it from &lt;a rel="noreferrer noopener" href="https://github.com/amitbiswas06/tartist-elementor-extension"&gt;GitHub&lt;/a&gt; page.&lt;/p&gt;

&lt;p&gt;Let me know if you have more ideas on this function.&lt;/p&gt;

&lt;h3&gt;Resources&lt;/h3&gt;

&lt;ol&gt;&lt;li&gt;&lt;a rel="noreferrer noopener" href="https://developer.wordpress.org/reference/functions/get_posts/"&gt;All about get_posts function.&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;

</description>
      <category>php</category>
      <category>wordpress</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Grid Carousel with "Tiny Slider"</title>
      <dc:creator>Amit Biswas</dc:creator>
      <pubDate>Fri, 01 May 2020 07:18:00 +0000</pubDate>
      <link>https://dev.to/amitbiswasta/grid-carousel-with-tiny-slider-44lf</link>
      <guid>https://dev.to/amitbiswasta/grid-carousel-with-tiny-slider-44lf</guid>
      <description>&lt;p&gt;Discover how to create a grid carousel with tiny slider script. Plain JavaScript used to create this carousel as the tiny slider itself using vanilla js. You can use this code in any application like WordPress and create a dynamic grid carousel with it. You can find the tutorial of this pen here:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/amitbiswas06/embed/RwWomrr?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codepen</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
