{"id":5112,"date":"2018-01-03T10:01:41","date_gmt":"2018-01-03T15:01:41","guid":{"rendered":"https:\/\/themespress.ca\/?p=5112"},"modified":"2022-10-01T14:43:50","modified_gmt":"2022-10-01T18:43:50","slug":"display-users-liked-articles-in-account","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/display-users-liked-articles-in-account\/","title":{"rendered":"Display User&#8217;s Liked Articles in Their Account"},"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 show you how to display the articles liked by a user in their account. We need to create a query that will display the posts with the post meta &#8220;like&#8221; and its value are equal to the WordPress user&#8217;s ID.<\/p>\n<p>Obviously, to be make this function works, we need a Like system so users can like posts such as this one: <a href=\"https:\/\/github.com\/JonMasterson\/WordPress-Post-Like-System\" target=\"_blank\" rel=\"noopener\">WordPress Post Like System<\/a><\/p>\n<p>You also need a user account and do not forget the conditions in the user&#8217;s profile page.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-3112967\" 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-3112967\" 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 \nif ( ! defined( &#039;ABSPATH&#039; ) ) exit; \/\/ Exit if accessed directly \nif ( is_user_logged_in()) : \nglobal $current_user; \nwp_get_current_user();\n$user = $current_user-&gt;ID; \n?&gt;\n<\/pre><\/div><h2 style=\"text-align: left;font-family:Abril Fatface;font-weight:400;font-style:normal\" class=\"vc_custom_heading vc_do_custom_heading\" >Display posts liked by the user<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>In their user account, we will display the list of recently liked post. This create this query, we fist need to fetch the user&#8217;s ID with <strong>get_current_user_id()<\/strong> via WordPress&#8217; get_currentuserinfo(). Next, the <strong>$types<\/strong> variable with fetch the required Post Type <strong>(In our example it&#8217;s Listings. Simply replace it by your Post Type or article post)<\/strong>. We will display 10 and then we&#8217;ll run a <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Meta_Query\" target=\"_blank\" rel=\"noopener\">meta_query<\/a> that will match the author&#8217;s ID and the post meta key.<\/p>\n<p>Now we will loop. We implement an incremented ranking with <strong>$i<\/strong>. Here&#8217;s our query:<br \/>\n(The user_liked key must be changed by your post meta key representing the Like)<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-2648155\" 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-2648155\" 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\t$author_id = get_current_user_id();\n\t$types = &#039;listings&#039;;\n\t$args = array(\n\t  &#039;numberposts&#039; =&gt; &#039;10&#039;,\n\t  &#039;post_type&#039; =&gt; $types,\n\t  &#039;meta_query&#039; =&gt; array (\n\t\tarray (\n\t\t  &#039;key&#039; =&gt; &#039;_user_liked&#039;,\n\t\t  &#039;value&#039; =&gt; $author_id,\n\t\t  &#039;compare&#039; =&gt; &#039;LIKE&#039;\n\t\t)\n\t  ) ); \n\t$like_query = new WP_Query( $args );\n\tif ( $like_query-&gt;have_posts() ) :\t?&gt;\n\t&lt;ul class=&quot;user-likes&quot;&gt;\n\t\t&lt;?php $i = 1; while ( $like_query-&gt;have_posts() ) : $like_query-&gt;the_post(); ?&gt;\n\t\t\t &lt;li&gt;\n\t\t\t\t &lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; title=&quot;&lt;?php the_title_attribute(); ?&gt;&quot;&gt;\n\t\t\t\t &lt;?php echo $i; ?&gt; &lt;?php _e(&#039;-&#039;); ?&gt;\n\t\t\t\t &lt;?php the_title(); ?&gt;\n\t\t\t\t&lt;\/a&gt;\n\t\t\t&lt;\/li&gt;\n\t\t&lt;?php $i++; endwhile; ?&gt;\n\t&lt;\/ul&gt;\n\t&lt;?php else : ?&gt;\n\t&lt;p&gt;&lt;?php _e( &#039;Pas de J&amp;#39;aime&#039;, &#039;via-entreprises&#039; ); ?&gt;&lt;\/p&gt;\n\t&lt;?php \n\tendif; \n\twp_reset_postdata(); \n?&gt;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will show you how to display the articles liked by a user in their account. We need to create a query that will display the posts<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/display-users-liked-articles-in-account\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5114,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[322],"class_list":["post-5112","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\/5112","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=5112"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5112\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5114"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}