how to store wordpress loop in array?

Use get_posts instead – You can use the function get_posts() to get all the posts as array. This function accepts almost all parameters that of query post’s. Example – $args = array( ‘numberposts’ => 3,’category’ => 3 ); $homePost = get_posts( $args ); $one = $homePost[0]; $two = $homePost[1]; $three = $homePost[2]; //print_r($one); // lets … Read more

number of posts per page + random + exclude categories

pre_get_posts in your child themes functions file is the best in my opinion. add_action( ‘pre_get_posts’, ‘wpsites_custom_get_posts’ ); function wpsites_custom_get_posts( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( ‘cat’, ‘-182,-184’ ); $query->set( ‘posts_per_page’, ’12’ ); $query->set( ‘orderby’, ‘RAND’ ); } } This method is more flexible in my opinion.

Proper syntax to add boolean in array()

Your code fails because: array( ‘cat’ => -9, -4 ); is the same as: array( ‘cat’ => -9, -4 ); The -4 is its own element, not a part of the ‘cat’ argument. The correct way to do what you tried to do would be: array ( ‘cat’ => array( -9, -4 ) ); But … Read more

post_parent array doesn’t work

It’s not recommended to use query_posts(), check for example this warning note in the Codex: 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 a page by … Read more

Each post is showing twice in my custom query…?

There is really no reason to use raw sql to query posts. You can accomplish just about any type of query using the WordPress API. For full reference see the codex, Function Reference query posts. Try the following: global $post; $args=array( ‘post_type’ => ‘tutorial’, ‘orderby’ => ‘comment_count’, ‘posts_per_page’ => 10 ); $posts= query_posts($args); foreach ($posts … Read more

Why this code causes infinite loop?

It was correctly pointed out what the issue is, but I also want to explain it so you have better idea of mechanics: when you use query_posts() it writes down generated WP_Query object into global $wp_query variable; have_posts() and the_post() functions are wrappers for methods of same name called using global $wp_query object; so what … Read more