Order posts using a custom array

You would need to build your own query and pass it to query_posts i.e. if your array of post ids is: array (10,21,1) // corresponding to post ids. then you can query this way: select * from wp_posts where `ID` in (10, 21, 2) order by field(ID,10,21,2) This will give you a result set in … Read more

making random query button using $_GET

You cannot use the direct class methods have_posts() or the_post() unless you’re working with the main query. In order to modify the main query you must use query_posts. If you want to create a new query object you need to call those methods from the new query object as Rarst showed in his example. So … Read more

get_meta_data within a loop

query_posts() is for main query, it would be more wise to use get_posts() function to get you events. // Modify get_posts args for your needs $events = get_posts( array ( ‘post_type’ => ‘events’ ) ); if ( $events ) { echo ‘<ul>’; foreach ( $events as $event ) { $event_date = get_post_meta( $event->ID, ‘event-date’, true … Read more

How do I make archives.php for one category only?

First: don’t use query_posts(), ever. Second, to create a category archive index page template for a specific category, refer to the Codex entry for the Template Hierarchy: category-{slug}.php category-{id}.php category.php archive.php index.php So, if you have a category, ‘foobar’, with a category ID of 1, you could do either of the following: category-foobar.php category-1.php And … Read more

Show individual author rank from query of cumulative post view count for all authors

First you can replace ‘posts_per_page’ => ‘10000000000’ with ‘posts_per_page’ => -1 to remove the limit. If you want to show the user rank, you can add a $rank counter in the foreach loop: $rank=0; // output the result: user, total postview count, latest post foreach ($output as $user){ $rank++; $profile = get_userdata($user[‘id’]); $query = get_posts( … Read more

ACF Custom Field WP_Query, but need to get all posts, if field doesn’t exist

You can get both with an OR meta_query that also checks if the key does not exist: ‘meta_query’ => array( ‘relation’ => ‘OR’, array( ‘key’ => ‘event_date’, ‘compare’ => ‘NOT EXISTS’, ), array( ‘key’ => ‘event_date’, ‘value’ => date(“Ymd”, time()), ‘compare’ => ‘>’, ), ), ‘orderby’ => array( ‘meta_value_num’ => ‘DESC’, ‘date’ => ‘ASC’, ),

Unable To Get Published posts using query_posts

First off, you probably shouldn’t be using query_posts, which modifies WordPress’ main loop and is unsuited for pretty much any purpose (read When should you use WP_Query vs query_posts() vs get_posts()? for more info). There are numerous reasons why query_posts wouldn’t work as expected in this case. WordPress has two proper approaches to fetching posts: … Read more

Adding Variables to post query

As toscho said, you can modify the query in the pre_get_posts hook. That hook gets the query object passed as an argument, so you don’t have to read a global variable. add_action( ‘pre_get_posts’, ‘wpse12692_pre_get_posts’ ); function wpse12692_pre_get_posts( &$wp_query ) { if( isset( $_SESSION[‘size’] ) && $_SESSION[‘size’] != ‘all’ ) { $wp_query->query_vars[‘meta_query’] = array( ‘key’ => … Read more