Randomize post list with some posts with specific ACF value in the top 50%

Issue with the way you are unsetting sticky posts from the $post_list array and then reinserting them at the specified positions and should work correctly like below code structure. Removed the unnecessary $position variable in the foreach loop. Changed the way sticky items are inserted back into the $post_list array using array_unshift to add them … Read more

Why does ‘get_the_ID’ work outside of the loop?

The get_the_ID() function uses the global $post variable, which outside of the loop is sometimes equal to get_queried_object_id(). Within the loop, the $post variable is set to the current post within the loop. As to why get_queried_object_id() is recommended outside of the loop, is because it does not use the global $post variable, so is … Read more

Display posts on a custom page from only a specific category?

The argument isn’t category, it is cat. Your query fails because you are using an argument that doesn’t exist. $args = array( ‘post_type’ => ‘post’ , ‘orderby’ => ‘date’ , ‘order’ => ‘DESC’ , ‘posts_per_page’ => 6, ‘cat’ => ‘3’, ‘paged’ => get_query_var(‘paged’), ‘post_parent’ => $parent ); $q = new WP_Query($args); if ( $q->have_posts() ) … Read more

How to show related posts by detecting the current category?

The question has already been asked and the answer has been posted too, How to display related posts from same category? Add this code inside your single.php after a loop wherever you want to show related post, <?php $related = get_posts( array( ‘category__in’ => wp_get_post_categories($post->ID), ‘numberposts’ => 5, ‘post__not_in’ => array($post->ID) ) ); if( $related … Read more

When to use WP_query(), query_posts() and pre_get_posts

You are right to say: Never use query_posts anymore pre_get_posts pre_get_posts is a filter, for altering any query. It is most often used to alter only the ‘main query’: add_action(‘pre_get_posts’,’wpse50761_alter_query’); function wpse50761_alter_query($query){ if( $query->is_main_query() ){ //Do something to main query } } (I would also check that is_admin() returns false – though this may be … Read more

tech