Getting a custom field in the middle of a loop

You should not be using query_posts at all, it is just problematic and is prone to failure Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of … Read more

Change query from cat id to slug or name?

Lets start by removing the query_posts() from your code since you already have it’s better brother new WP_Query. Then what you will need is to use the category_name param on your WP_Query. You should always search on the WP_Query page before you ask here, you can find more referencers there. Here is how @speedypancake resolved … Read more

Wrong post title displayed from loop

Welcome the the WordPress Stack Exchange and congrats on your first post! First off, you may not want to use query_posts() here. Second off – the_post() needs to be before the_title() as it sets up all the posts functions such as the_title(), the_permalink() etc. and queues the next post in your while loop. Your Loop … Read more

Update status of all posts in a category

I am sure there’s an easier way to accomplish this, but this how I would’ve done. Query all posts with category X and status X Update query results with status Y. $target_category = ‘news’; $change_status_from = ‘draft’; $change_status_to = ‘publish’; $update_query = new WP_Query(array(‘post_status’=>$change_status_from, ‘category_name’=>$target_category, ‘posts_per_page’=>-1)); if($update_query->have_posts()){ while($update_query->have_posts()){ $update_query->the_post(); wp_update_post(array(‘ID’=>$post->ID, ‘post_status’=>$change_status_to)); } } You can … Read more

Loop for sticky posts

The issue is that you are using the function query_posts this only queries the default result on that page. It’s advised that you use wp_query instead, it just eliminates the margin for error or unexpected results. You can create a new query like below, and specify explicitly how many posts to return: <?php $sticky = … Read more