Tag Info

Hot answers tagged

7

You can try a trick with querying post data directly and setting filter field of post objects to sample before passing it to get_permalink() to reduce memory usage. See get_permalink memory usage issue for detailed reasoning behind it.


3

To print just the total number of comments for a given post ID, use the count argument: echo get_comments( array ( // post ID 'post_id' => 149, // return just the total number 'count' => TRUE ) ); To get the total number of all comments of all posts on the current page, you can use the comment_count property ...


3

You might try adding this to your array: 'nopaging' => true, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false It seems pretty self-explanatory, but essentially you're not querying all post variables and just the stuff you need.


2

Without seeing the broken code it is hard to say, but your filter should look something like this: function pregp_wpse_97354($qry) { if (is_front_page() && is_main_query() && is_user_logged_in()) { $qry->set('cat',2); $qry->set('posts_per_page',5); $qry->set('orderby','date'); $qry->set('order','DESC'); } } ...


2

To copy a post from one blog to another you can do something like this: function copy_post_to_blog($post_id, $target_blog_id) { $post = get_post($post_id, ARRAY_A); // get the original post $post['ID'] = ''; // empty id field, to tell wordpress that this will be a new post switch_to_blog($target_blog_id); // switch to target blog ...


2

Programmer Dan, mah man! Let's get you started on custom SELECT queries using the $wpdb global. The Codex has a great entry on Displaying Posts using a Custom Select Query. If you make use of setup_postdata() you can loop through the results as though you are sitting in the standard Wordpress loop: global $wpdb; $sitemap_query = " SELECT ...


2

I did something similar, the technique you need to use is called a meta query. Here is the query I wrote to get posts based on a date value stored as a custom field meta value. query_posts( array( 'post_type'=>'post', 'order'=>'ASC', 'orderby'=>'meta_value_num', 'meta_key'=>'date_event', 'posts_per_page'=> -1, 'meta_query' => array( ...


2

The WordPress function switch_to_blog() expects an integer as an input parameter. You can read more about it in the Codex: http://codex.wordpress.org/Function_Reference/switch_to_blog Please try this kind of structure instead: <?php $original_blog_id = get_current_blog_id(); // get current blog $bids = array(1,2); // all the blog_id's to loop through ...


2

When you initiate a Loop, split it up like so: <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <!-- do stuff ... --> <?php endwhile; ?> <?php endif; ?> Anything inside the if statement but outside the while statement will run if have_posts is true, but will not be ...


2

query_posts() is a horrible function and shouldn't be used. Also it doesn't has much to do with loading template, since it was originally designed to be used in template. Should not be used. If you are looking for logic of how WP picks template files from theme to load see Template Hierarchy. If you just want to load some [part of] template manually, ...


1

Perhaps you can take a look at this question and the answers and check if you really want to use query_posts() as it is not recommended while dealing with posts pagination. Why not use pre_get_posts action instead? Or you can try something like this: <?php $custom_query = new WP_Query(array( 'post_status' => 'publish', 'post_type' ...


1

There is no term_id parameter. You need a tax_query. $args = array( 'post_type' => 'my_post', 'posts_per_page' => 6, 'tax_query' => array( array( 'taxonomy' => 'yourtaxname', 'field' => 'id', 'terms' => 1 ) ) ); $query = new WP_Query( $args ); Notice that I've use new ...


1

I think this must have been solved many times here on WordPress Answers. You could also check out the examples in the Time parameters part in Codex for WP_Query. Here are two of them (slightly modified to your needs) Example 1: // Create a new filtering function that will add our where clause to the query function filter_where( $where = '' ) { // ...


1

From the WP_Query Time Parameters section: Returns posts for just the current week: $week = date('W'); $year = date('Y'); $query = new WP_Query( 'year=' . $year . '&w=' . $week );


1

You're querying two different taxonomies- in your wpbd query you reference product_cat. in the call to query_posts you reference the default category taxonomy via the cat argument. as an aside, you should be using WP_Query instead of query_posts.


1

First, don't use query_posts. From the Codex: query_posts() is the easiest, but not preferred or most efficient, way to alter the main query that WordPress uses to display posts. It does this by putting the main query to one side, and replacing it with a new query. To clean up after a call to query_posts, make a call to wp_reset_query(), and the ...


1

WP_Query has a "return fields" paramater that looks like this: $args = array( 'fields' => 'ids' ); $query = new WP_Query( $args ); When used this way, WP_Query only returns the post IDs, not the entire post object. Then you can just use the get_permalink(), get_the_title(), and other assorted WordPress functions to retrieve your content based on the ...


1

I've done something in the past using custom fields. Maybe this can get you going in the right direction? This is done directly after the <?php while ( have_posts() ) : the_post(); ?> <?php $currentdate = date("Ymd"); $expirationdate = genesis_get_custom_field('_racedate'); $expirestring = str_replace("-","",$expirationdate); if ...


1

This is what i did: I run another query without pagination like this: $newQuaryVars = '&posts_per_page=99999999999999&post_type=post'; $posts = query_posts($query_string .$newQuaryVars); $categoriesList = array(); $categoriesAmounts = array(); Then in the while have posts i did the following: while ( have_posts() ) : the_post(); ...


1

$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'post__in', 'post__in' => array(883, 563, 568, 106); ); Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in



Only top voted, non community-wiki answers of a minimum length are eligible