How do I create a random post that will last for a day

Here is some code doing what you’ve asked and using the ideas others have highlighted: <?php if ( false === ( $totd_trans_post_id = get_transient( ‘totd_trans_post_id’ ) ) ) { $args = array(‘numberposts’ => 1, ‘orderby’ => ‘rand’); $totd = get_posts($args); $midnight = strtotime(‘midnight +1 day’); $timenow = time(); $timetillmidnight = $midnight – $timenow; echo $midnight; … Read more

query_posts exclude a meta key

I’ve never had any luck getting the meta compare to work either–but I came up with a workaround for this exact situation (having “featured” items at the top of the page). First, you probably shouldn’t be using query_posts for both queries. You should use a custom query for at least the first one. Then, while … Read more

How to set posts per page using WP_Query()

query_posts will do the query again (destroy current wp_query and create a new one and then doing the query with the parameters you pass to it) To get the same behaviour after setting the new parameter with set_var you need to query the database again using something like this $wp_query->set(‘posts_per_page’, 1); $wp_query->query($wp_query->query_vars);

Order posts by ID in the given order

Starting in WordPress 3.5, the orderby parameter will allow the value post__in to sort by the order of the post__in parameter, just like in your example. It may not be ideal to wait or require 3.5+, but this will almost certainly be the best and easiest way to do what you’re looking to do. Here’s … Read more

Custom Taxonomy not working with posts_per_page in new WP_query (pagination problem)

should it be.. $paged = (get_query_var(‘page’)) ? get_query_var(‘page’) : 1; WP_Query in codex: Pagination Note: You should set get_query_var( ‘page’ ); if you want your query to work with pagination. Since WordPress 3.0.2, you do get_query_var( ‘page’ ) instead of get_query_var( ‘paged’ ). The pagination parameter ‘paged’ for WP_Query() remains the same.

Help to condense/optimize some working code

Ok I figured it out. Here’s the optimized code: // Show only posts related to current user add_action(‘pre_get_posts’, ‘query_set_only_author’ ); function query_set_only_author( $wp_query ) { global $current_user; if( is_admin() && !current_user_can(‘edit_others_posts’) ) { $wp_query->set( ‘author’, $current_user->ID ); add_filter(‘views_edit-post’, ‘fix_post_counts’); } } // Fix post counts function fix_post_counts($views) { global $current_user, $wp_query; unset($views[‘mine’]); $types = array( … Read more

Query Posts in a Predefined Order

If the query is only for a small number of posts, then as linked to by Alex you can sort in php. However, this does not scale well. As suggested by Kovshenin – a better alternative is to use posts_orderby filter: $post_ids = array(83,24,106,2283,14); $args = array( ‘post_type’ => ‘post’, ‘post__in’ => $post_ids, ‘numberposts’ => … Read more

Display all posts from specific categories on a page

I’d Advise adding the arg of the category in an array. And don’t use query_posts. Also showposts is deprecated use posts_per_page instead. $args = array ( ‘cat’ => array(2,6,9,13), ‘posts_per_page’ => -1, //showposts is deprecated ‘orderby’ => ‘date’ //You can specify more filters to get the data ); $cat_posts = new WP_query($args); if ($cat_posts->have_posts()) : … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)