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

Custom category search box for WordPress

In every template file of your theme, even in the header.php you can put <?php $term = null; $btn = __(‘Search’); if ( is_category() ) { $term = get_queried_object(); } ?> <form method=”get” id=”search_form” action=”<?php echo home_url(); ?>”> <div> <input type=”text” value=”” name=”s” id=”s” /> <?php if ($term) { $btn = sprintf( __(‘Search in %s’), … Read more

Exclude posts with empty post_content in wp_query

This isn’t possible with a standard WP query, and you’ll have to leverage the use of posts_where before the WP_Query is called. function the_dramatist_filter_where($where=””){ $where .= ” AND trim(coalesce(post_content, ”)) <>””; return $where; } In the above, we’re simply only selecting posts where the column post_content isn’t empty. Then add the filter. add_filter(‘posts_where’, ‘the_dramatist_filter_where’); Now … Read more