Need to know custom code to display random and most viewed posts in wordpress posts and pages

A standard WordPress install doesn’t keep track of visits to pages/posts. So without a plugin there’s no way to know which posts/pages are visited most often. As to finding random posts that’s as simple as: $args = array( ‘post_type’ => array( ‘post’, ‘page’, ‘my-custom-post-type’ ) ‘orderby’ => ‘rand’, ); $query = new WP_Query ($args);

How to get the latest URL of my blog?

easy to do with get_posts // search the last post $posts = get_posts([ “posts_per_page” => 1, ]); foreach ($posts as $p) { // display the permalink echo esc_html(get_permalink($p)); } documentation of get_posts : https://codex.wordpress.org/Function_Reference/get_posts

Custom Fields for Author Box

Add the new custom field to the author profile function my_epl_custom_user_contact( $contactmethods ) { $contactmethods[‘custom’] = __( ‘Custom’, ‘easy-property-listings’ ); return $contactmethods; } add_filter (‘user_contactmethods’,’my_epl_custom_user_contact’,10,1); Create the HTML output of the Custom Fields function my_epl_get_custom_author_html($html=””) { global $epl_author; if ( $epl_author->custom != ” ) { $html=” <a class=”epl-author-icon author-icon custom-icon-24″ href=”http://custom.com/” . $epl_author->custom . ‘” … Read more

Querying another post category to match current post and display in loop

if you are adding the custom query into the single template, try working with get_the_category() to get the cat_id for your query; also, get_posts() does not work with if( have_posts() ) etc. example code, using a custom query: <?php $cat = get_the_category(); $cat_id = $cat[0]->term_ID; $cpt_match_query = new WP_Query( array( ‘posts_per_page’ => 2, ‘post_type’ => … Read more

Random posts that always include a particular post?

Try this code:: post__in (array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. <?php $parent = $post->ID; $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; $query_args = array( ‘post_type’ => ‘page’, ‘post_in’ => array(‘post_id_to_include’), … Read more