Modify main WordPress loop with a parse_query filter

Ultimately all meta queries get run through _get_meta_sql. wp-includes/meta.php Line 402 – 403 in the _get_meta_sql function The following coditional statement runs for any meta queries. if ( empty( $meta_key ) && empty( $meta_value ) ) continue; There’s a ticket here for this which outlines what we should be able to do. http://core.trac.wordpress.org/ticket/16735 What you … Read more

Determine if more posts are available than was asked for in `query_posts()`?

First of all: do not use query_post! For more information read: When should you use WP_Query vs query_posts() vs get_posts()? Use the WP_Query class to fetch your products, also pay attention that the showposts argument is deprecated, use posts_per_page instead: $args = array( ‘post_type’ => ‘product’, ‘taxonomy’ => ‘product_cat’, ‘term’ => $idObj->slug, // category slug … Read more

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);

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

How to query post by user role?

I haven’t really messed around with custom post queries before, but here is my try at a solution: function get_posts_by_author_role($role) { global $wpdb; return $wpdb->get_results( “SELECT p.* FROM {$wpdb->posts} p, {$wpdb->usermeta} u” .” WHERE p.post_type=”post”” .” AND p.post_status=”publish”” .” AND u.user_id = p.`post_author`” .” AND u.meta_key = ‘wp_capabilities'” .” AND u.meta_value LIKE ‘%\”{$role}\”%'” ); } … Read more

Display/query post formats

You have a lot of options for displaying using the “Post Formats” feature: for example in an index.php loop you can decide what to show based on the post format using has_post_format() function : if ( has_post_format( ‘aside’ )) { echo the_content(); } elseif ( has_post_format( ‘chat’ )) { echo ‘<h3>’; echo the_title(); echo ‘</h3>’; … Read more

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