WordPress Plugin manipulate have_posts()

The standard query for posts uses orderby => ‘post_date’ and order => ‘DESC’. The best candidate for arbitrary sort of posts would be orderby => ‘menu_order’ and order => ‘ASC’. The menu_order is not generally used for posts but it is safe to be used. As suggested by Pieter Goosen use action hook pre_get_posts: add_action( … Read more

different for loop if screen resolution < 1000px

query_posts is a PHP function, not a javascript function, it’s executed on the server, and the resulting output is sent to the browser. By the time the browser gets the chance to run your if( $(window).width() < 2000 ) { check, it’s already too late. The server has finished its PHP script, sent it across, … Read more

Exclude first post (sticky or not) from the loop using query_posts()

You shouldn’t really need to “exclude” anything. Just make use of the built in loop tracking. WP_Query will “remember” where you left off. For example: if (have_posts()) { while (have_posts()) { the_post(); echo ‘firstpost##’; the_content(); echo ‘##endfirstpost’; break; } } // do some other stuff if (have_posts()) { while (have_posts()) { the_post(); echo ‘otherposts##’; the_content(); … Read more

Exclude current custom post on single post

Refer to the WP_Query Codex page for a full list and explanation of parameters. The one you want in this case is post__not_in. $recent = new WP_Query( array( ‘post_type’ => ‘works’ ‘posts_per_page’ => 5, ‘post__not_in’ => array( $currentID ) ) );

Cannot retieve the_content() and the_author() – both returning empty strings

get_the_content and the_author have to be in a loop so you would need to use other functions to get the content you need global $wp_query; $post = $wp_query->post; $page_id = $post->ID; // page ID $page_object = get_page( $page_id ); // page stuff $author_id = $post->post_author; // author ID $page_content = $page_object->post_content; $author_name = get_the_author_meta( ‘display_name’, … Read more

Is wp_reset_postdata() redundant after the main loop?

The first use of wp_reset_postdata() is not redundant its unnecessary. Per the codex Use this function to restore the global $post variable of the main query loop after a secondary query loop using new WP_Query. It restores the $post variable to the current post in the main query. So only use it after your secondary … Read more

Possible to alternate between two loops

You need two queries for this. Let’s say the ID of the featured category is 5. Then you set up two queries retrieving 3 posts each: $query1 = new WP_Query( array( ‘cat’ => ‘5’, ‘posts_per_page’ => 3 ) ); $query2 = new WP_Query( array( ‘cat’ => ‘-5’, ‘posts_per_page’ => 3 ) ); Now you have … Read more