{"id":6294,"date":"2022-06-24T14:25:53","date_gmt":"2022-06-24T18:25:53","guid":{"rendered":"https:\/\/themespress.ca\/?p=6294"},"modified":"2022-09-09T06:07:06","modified_gmt":"2022-09-09T10:07:06","slug":"creating-query-post-shortcode","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/creating-query-post-shortcode\/","title":{"rendered":"Creating a Query Post Shortcode for WordPress"},"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 create a shortcode that will return the results of a WordPress query.<\/p>\n<p>We will be creating a <a title=\"Add Shortcode\" href=\"https:\/\/codex.wordpress.org\/Function_Reference\/add_shortcode\" target=\"_blank\" rel=\"noopener\">add_shortcode<\/a> shortcode that will contain the shortcode name and function as parameters.<br \/>\nWith this example, we will call a query for a Video post type sorted by number of likes.<\/p>\n<p>We edited the get option inside the shortcode if you have options. Then we run the new WP_Query.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-6685796\" 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-6685796\" 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\/\/ Creation du shortcode permettant de lister la liste des videos class&eacute;es par nombre de likes\nadd_shortcode( &#039;viavideoslikes&#039;, &#039;via_videos_list_likes_shortcode&#039; );\nfunction via_videos_list_likes_shortcode( $atts ) {\n\tob_start();\n\t$options = get_option(&#039;via_video_name&#039;);\n\t$query = new WP_Query( array(\n\t\t&#039;post_type&#039;            =&gt; &#039;viavideos&#039;,\n\t\t&#039;meta_key&#039;             =&gt; &#039;_post_like_count&#039;,\n\t\t&#039;orderby&#039;              =&gt; &#039;meta_value_num&#039;,\n        &#039;order&#039;                =&gt; &#039;DESC&#039;,\n\t\t&#039;posts_per_page&#039;       =&gt; -1,\n\t\t) );\n\tif ( $query-&amp;gt;have_posts() ) { \n\t?&gt;\n\t\n\t\n\t&lt;?php while ( $query-&gt;have_posts() ) : $query-&gt;the_post(); ?&gt;\n\n\t\n        \/\/ Afficher ici votre contenu dynamique dans vos contenants div\n\n\n\t&lt;?php endwhile; wp_reset_postdata(); ?&gt;\n\t&lt;?php $myvariable = ob_get_clean();\n\treturn $myvariable;\n\t}\n}<\/pre><\/div><h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Creating a Query Post Shortcode for WordPress<\/h2><div id=\"ts-enlighterjs-container-5043154\" 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-5043154\" 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=\"\">[viavideoslikes]<\/pre><\/div><h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Or in a template page<\/h2><div id=\"ts-enlighterjs-container-6462409\" 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-6462409\" 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 echo do_shortcode(&#039;[viavideoslikes]&#039;); ?&gt;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will create a shortcode that will return the results of a WordPress query. We will be creating a add_shortcode shortcode that will contain the shortcode name<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/creating-query-post-shortcode\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4535,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318,322],"class_list":["post-6294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions","tag-query"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6294","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=6294"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6294\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4535"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}