{"id":6393,"date":"2016-02-11T13:39:41","date_gmt":"2016-02-11T18:39:41","guid":{"rendered":"https:\/\/themespress.ca\/?p=6393"},"modified":"2022-09-11T13:56:20","modified_gmt":"2022-09-11T17:56:20","slug":"query-post-type-by-wordpress-authors","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/query-post-type-by-wordpress-authors\/","title":{"rendered":"Query Post type by WordPress Authors"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial we will do a Query Post type for each WordPress author. In the example below, our Post Type will be events. You need to give access to your authors via the Events <a href=\"https:\/\/formidablepro.com\/?aff=viamultimedia&amp;p=formidable-pro\" target=\"_blank\" rel=\"noopener\">Formidable Forms<\/a>.<\/p>\n<p>First thing we need to do is create a new template: <strong>tpl-evenements.php<\/strong>.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Query Post type by WordPress Authors<\/h2>\n\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Make sure to create their identity inside the template. The user will need to be logged in their WordPress account.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n\n<div id=\"ts-enlighterjs-container-4885193\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-4885193\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \/* Template Name: Les Evenements *\/ ?&gt;<\/pre><\/div>\n<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Afterwards, feel free to add it in your header, footer,&#8230; .<br \/>\nHere&#8217;s the query you&#8217;ll need contained with your content DIV in order see the Events by authors.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-4205435\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-4205435\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \n$author_id = get_current_user_id(); \n$args = array( \n&#039;posts_per_page&#039;            =&gt; 5,  \/\/ Limit to 5 posts\n &#039;post_type&#039;                 =&gt; &#039;evenements&#039;,  \/\/ Query for the default Post type\n &#039;author&#039;                    =&gt; $author_id  \n    );\n $last_five_posts = get_posts( $args );\n foreach ( $last_five_posts as $post ) : setup_postdata( $post ); ?&gt;\n     \n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Ici le contenu des r&eacute;sultats a afficher title, content, category, ect... \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n     \n&lt;?php endforeach; wp_reset_postdata(); ?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>It is vital to close the query with the wp_reset_postdata(); function. Log into your WordPress account then create a new Events page and assign it to your Events template.<\/p>\n<p>The Query Post type by WordPress authors will display properly when your users are connected.<\/p>\n<p>Now we will twaek the query. If you want to display the author&#8217;s post type when clicking on an author link and display your author&#8217;s posts, you need to replace <strong>get_current_user_id();<\/strong> with <strong>$post->post_author;<\/strong><\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-7183366\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-7183366\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">\n&lt;?php \n$author_id = $post-&amp;gt;post_author;\n$args = array(\n&#039;posts_per_page&#039;         =&gt; 5,  \/\/ Limit to 5 posts\n&#039;post_type&#039;              =&gt; &#039;evenements&#039;,  \/\/ Query for the default Post type\n&#039;author&#039;                 =&gt; $author_id\t\n);\n$last_five_posts = get_posts( $args );\nforeach ( $last_five_posts as $post ) : setup_postdata( $post ); ?&gt;\n \n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Ici le contenu des r&eacute;sultats a afficher title, content, category, ect... \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n \n&lt;?php endforeach; wp_reset_postdata(); ?&gt;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial we will do a Query Post type for each WordPress author. In the example below, our Post Type will be events. You need to give access to<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/query-post-type-by-wordpress-authors\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4498,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[322],"class_list":["post-6393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-query"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6393"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4498"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}