Tag Info

New answers tagged

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 ...


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, ...


0

For the solution you can use if statement as below put it in your sidebar, it will highlight the current post title. <ul> <?php $IDOutsideLoop = $post->ID; $args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' ); $myposts = get_posts( $args ); foreach($myposts as $post) : ?> <li <?php if(is_single() ...


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


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 ...


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 ...


0

You could use the wp_dropdown_categories() function and pass post_tag as taxonomy parameter. <?php wp_dropdown_categories( array( 'taxonomy' => 'post_tag' ) ); ?>


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 ...


0

$args = array( 'post_type' => 'product', 'numberposts' => -1, 'orderby' => 'meta_value_num', 'order' => get_query_var('order'), 'meta_key' => '_price' ); $lastposts = get_posts( $args );


0

$tax_query = array ( array ( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $cat_name ) ); $args = array( 'tax_query' => $tax_query, 'numberposts' => -1); $lastposts = get_posts( $args );


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( ...


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 ...


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 ...


0

Your question is not very detailed. It is hard to work out exactly what you are doing but I am assuming that you are trying to sort posts by a custom meta meta_key/meta_value. This the formula for that (annotated but basically lifted from the Codex): $args = array( 'post_type' => 'your_post_type', // I don't know what this is 'meta_key' => ...


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


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.


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 ...


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.


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


0

It is easy to get the number of posts in a category... $qry = new WP_Query(array('cat'=>2)); var_dump($qry->found_posts); ... but you could very easily have a lot of queries that way. Maybe something like this would help reduce that: $catid = 1; $qry = new WP_Query; $cat_count = array(); if (!isset($cat_count['cat-'.$catid])) { ...


0

You can order posts by custom taxonomy terms by adding a posts_clauses filter, see below answer: http://wordpress.stackexchange.com/a/14313/14499 Make sure that on functions.php you add both that function AND the call to add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 );, otherwise it won't work. Assuming annee is the custom taxonomy, this should ...


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'); } } ...


0

You are passing the cat parameter twice-- the second time is empty. $page_query = new WP_Query( 'post_type=post&cat=145'. '&posts_per_page=-1&cat' . // <-- here '&orderby=date&order=asc' ); Maybe it is just me, but I find those query-string-like parameters hard to read and hard to keep straight. I'd advise you to create ...


0

Disabling canonical redirect (crudely) gets the job done.



Top 50 recent answers are included