I have a page that lists posts grouped by category. Here is the current code:  &lt;?php  $cats = get_terms('category');  foreach ($cats as $cat) {     $cat_id= $cat-&gt;term_id;      echo "&lt;h2&gt;".$cat-&gt;name."&lt;/h2&gt;";      $custom_query = new WP_Query(         array(             'cat' =&gt; $cat_id,             'post_type' =&gt; 'my_post_type',             'posts_per_page' =&gt; -1,             'orderby' =&gt; 'title',             'order' =&gt; 'ASC'         )     ); ?&gt;     &lt;ul&gt;     &lt;?php if ($custom_query-&gt;have_posts()) : while ($custom_query-&gt;have_posts()) : $custom_query-&gt;the_post(); ?&gt;         &lt;li&gt;&lt;a href="&lt;?php the_permalink();?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;     &lt;?php endwhile; endif; ?&gt;     &lt;/ul&gt;  &lt;?php } ?&gt;   Is there a more efficient way to do this without so many queries? I found this related question: . However, I'm not sure how to implement Milo's suggestion or if it would be applicable.
27089
I am looking for a solution that allows me to print the following:  Cat 1        Cat 2        Cat 3  Post 1       Post 1       Post 1  Post 2       Post 2       Post 2  Post 3                    Post 3                            Post 4   EDIT  I am looking for something that will only require one database query! So if you have a foreach in your code followed by a new WP_Query then that is not what I am looking for (I am planning to put this onto my website's homepage).
14734
I'm writing my first WordPress theme and have a question about the use of The Loop in page templates. The page templates I've looked at all follow basically the same pattern (example from Twenty Twelve):  &lt;?php while ( have_posts() ) : the_post(); ?&gt; &lt;?php get_template_part( 'content', 'page' ); ?&gt; &lt;?php comments_template( '', true ); ?&gt; &lt;?php endwhile; // end of the loop. ?&gt;   But a page will only have one post associated with it so iterating through the posts seems unnecessary. My page template is more readable and seems to work fine:  &lt;?php the_post(); the_title('&lt;h1&gt;', '&lt;/h1&gt;'); the_content(); ?&gt;   Is this a good practice? Are there any downsides?
31232
I used the following code to give out the category name (without link) in a single post.  How would it be possible for the category name that is given out to only appears in small letters ?  I just think this has to be made with substring, but I dont get it :) Could somebody please help me :)  &lt;?php foreach((get_the_category()) as $category) { echo $category-&gt;cat_name . ' '; } ?&gt;
56352
I'm triyng to display hotels (taxonomy which i have created) by two 'orderby' with wp_query but i'm not success.  Results looks likes:  Hotel 1 :       2 persons          5 stars Hotel 2 :       2 persons          4 stars Hotel 3 :       2 persons          3 stars Hotel 4 :       3 persons          5 stars Hotel 5 :       5 persons          5 stars Hotel 6:        10 persons         2 stars Hotel 7:        10 persons         1 star   The first 'order by' is on the number of persons(the biggest in first) and the second 'order by' is on the rating in stars (so, if several hotels have the same number of persons, the hotel with the biggest stars rating would be in first).  Below my code. I have tried to add an array inside an array with meta_query but it doesn't work.  $args=             array(                                     'post_type' =&gt; 'hotel',                                     'Services' =&gt; $Services,                                     'Town' =&gt; $Town,                                      'meta_key' =&gt; 'Number_persons',                                     'orderby' =&gt; 'meta_value_num',                                     'order' =&gt; 'ASC',                                        'meta_query' =&gt; array(                                                            array(                                                                'meta_key' =&gt; 'Rating',                                                                'orderby' =&gt; 'meta_value',                                                                'order' =&gt; 'DESC',                                                               )                                                        )                  );   $the_query = new WP_Query( $args );   Thanks a lot :)
102168
I'm trying to create a archive list with only my "normal" post format articles (not link, aside, quote, etc formats).  How would I implement has_post_format( 'standard' ), or something similar, into the code below?  I haven't been able to find a query for get_posts that only requests specific format types.  &lt;?php         // Get the posts     $myposts = get_posts('numberposts=-1&amp;orderby=post_date&amp;order=DESC');      ?&gt;  &lt;?php foreach($myposts as $post) : ?&gt;     &lt;?php         // Setup the post variables     setup_postdata($post);      $year = mysql2date('Y', $post-&gt;post_date);     $month = mysql2date('n', $post-&gt;post_date);     $day = mysql2date('j', $post-&gt;post_date);     ?&gt;  &lt;p&gt;     &lt;span class="the_article"&gt;         &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;     &lt;/span&gt;     &amp;nbsp;&amp;nbsp;&amp;nbsp;     &lt;span class="the_day"&gt;         &lt;?php the_time('j F Y'); ?&gt;     &lt;/span&gt; &lt;/p&gt;  &lt;?php endforeach; ?&gt;   My php skills are at the beginner level at best, so any help would be much appreciated.
114158
I've found several similar question but none of them explains why the below is giving me nothing with ASC order. It works with DESC.  Basically it's a query that should list posts where the custom field values are equal or greater to the current date, then arrange it in reversed order, so the first post should be a post that's custom field are the closest the the current date.  &lt;?php query_posts($query_string . '&amp;showposts=4&amp;order=ASC&amp;post_type=custompost&amp;meta_key=mydate&amp;orderby=meta_value'); ?&gt;     &lt;?php if (have_posts()) : ?&gt;     &lt;?php while (have_posts()) : the_post(); ?&gt;          &lt;?php $todays_date = date("Y/m/j"); ?&gt;         &lt;?php $today = strtotime($todays_date); ?&gt;         &lt;?php $exp_date = get_post_meta('mydate'); ?&gt;         &lt;?php $expiration_date = strtotime($exp_date); ?&gt;         &lt;?php if ($expiration_date &gt;= $today) { ?&gt;              &lt;a href="&lt;?php the_permalink(); ?&gt;" rel="bookmark" title="&lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;      &lt;?php } else { ?&gt;     &lt;?php };?&gt;  &lt;?php endwhile; ?&gt; &lt;?php else : ?&gt; &lt;?php endif; ?&gt;    Also, I've tried many different queries but none of them worked, the above script is the latest I have.
101864
I am trying to fetch 1st category for each blog post that appears in the sidebar of any blogpost landing page. I tried multiple options to add the 2nd block code to the 1st block at the line &quot;.wp_get_post_categories ($post_id).&quot; but I could not find any solution. Please help me with that and if you need closer to the code just let me know. 1st Block $related_blog .=&quot;&lt;div class='related-blog'&gt;                             &lt;div class='row'&gt;                                 &lt;a href=&quot;.get_the_permalink().&quot;&gt;                                                                  &lt;div class='col-lg-4 col-md-4 col-xs-4 blog-image'&gt;                                         &lt;img src=&quot;.$image_related[0].&quot;&gt;                                     &lt;/div&gt;                                      &lt;div class='col-lg-8 col-md-8 col-xs-8 blog-text'&gt;                                         &lt;strong&gt;&lt;p class='related-blog-title'&gt;&quot;.get_the_title().&quot;&lt;/p&gt;&lt;/strong&gt;                                 &lt;/a&gt;                                 &lt;span class='related-blog-sidebar-content '&gt;&quot;.substr(get_the_content(),0,250).&quot;...&lt;/span&gt;                                                                  &lt;p class='related-blog-sidebar-category'&gt;                                 &quot;.wp_get_post_categories ($post_id).&quot;                                                                &lt;/p&gt;                                                                  &lt;a class='related-blog-sidebar-link' href=&quot;.get_the_permalink().&quot;&gt;Read More                                 &lt;/a&gt;                                      &lt;/div&gt;                             &lt;/div&gt;                         &lt;/div&gt;&quot;;  2nd Block &lt;?php $category = get_the_category(); ?&gt; &lt;a  href=&quot;&lt;?php echo get_category_link($category[0]-&gt;cat_ID); ?&gt; &quot;&gt; &lt;?php echo $category[0]-&gt;cat_name; ?&gt; &lt;/a&gt;
4512
I've got a custom loop that I'm using to display some Real Estate listings that will be available within 60 days. I'm calling it with the following function:   &lt;?php  $sixtydays = date('Y/m/d', strtotime('+60 days')); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $query = new PostsOrderedByMetaQuery(array(   'post_type' =&gt; array('post', 'real-estate'),   'meta_key' =&gt; 'Time Available',   'meta_compare' =&gt; '&lt;=',   'meta_value' =&gt; $sixtydays,   'paged' =&gt; $paged,   'orderby_meta_key' =&gt; 'Price',   'orderby_order'    =&gt; 'ASC' )); ?&gt; &lt;?php while ($query-&gt;have_posts()) : $query-&gt;the_post(); ?&gt;   While the loop works great, I can't get it to paginate. It shows the first 10 (my default) posts but doesn't show the pagination. The only way to display all posts is to show them on one page by adding 'posts_per_page' =&gt; -1, I have similar loops on other pages that have no problem paginating. The only difference with this one is that there are two meta keys that are filtering the posts.   I'm using WP Page Navi for this and the rest of my pages. I'm closing the loop and adding the pagination using the following code:   &lt;?php endwhile; // End the loop. Whew. ?&gt; &lt;?php wp_pagenavi(); ?&gt; &lt;?php wp_reset_query(); ?&gt;   How can I go about fixing this?
105801
I have been trying but its not working.  but if i try   &lt;?php echo do_shortcode("[mimo]"); ?&gt; &lt;?php echo do_shortcode("[mibaby]"); ?&gt;   it will work fine.  But to apply it in post content using [mimo] [mibaby]  it won't work.  Here is the Functions i am using...  function wpb_custom_new_menuw() {   remove_filter( 'the_content', 'wpautop' ); } add_shortcode('mibaby', 'wpb_custom_new_menuw');    function wpb_custom_new_menu() {   remove_filter( 'the_excerpt', 'wpautop' ); } add_shortcode('mimo', 'wpb_custom_new_menu');
19602
Could I theoretically use multiple php code areas / widgets in a page, each doing their own posts query and fetching with wp_query and get_posts() and use a page template that has no post loop?
41492
I'm trying to add some function to each loop called in the themes. What I'm doing is I add a function to count post view from here , to use the function I need to add this line &lt;?php setPostViews(get_the_ID()); ?&gt; to single.php, page.php, etc.  I want to know whether I can use some hook to automatically add the &lt;?php setPostViews(get_the_ID()); ?&gt; line without hard code it for each single.php, page.php files.
69577
I am trying to output a list of stockists in two columns from left to right, sorted alphabetically:  A | B  C | D  E | F  G | H  Currently, the code below is sorting half the posts alphabeticly and then starting half way through with the rest alphabeticly:  A | C  E | G  B | D  F | H  Any idea on where I am going wrong?  &lt;?php  query_posts(  array( 'order' =&gt; 'ASC' , 'orderby' =&gt; 'title' , 'posts_per_page' =&gt; -1 , 'post_type' =&gt; 'stockist_directory', 'regions' =&gt; 'metropolitan' ) ); if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) == 0) : $wp_query-&gt;next_post(); else : the_post(); ?&gt;  &lt;div id="left-column"&gt; &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;   &lt;?php the_field('shop_address'); ?&gt;&lt;br/&gt;   &lt;?php the_field('suburb'); ?&gt;, &lt;?php the_field('postcode'); ?&gt;&lt;br/&gt;   Phone: &lt;?php the_field('phone_number'); ?&gt;&lt;br/&gt;   &lt;a href="&lt;?php the_field('website_address'); ?&gt;"&gt;Website&lt;/a&gt; &lt;/div&gt;  &lt;?php endif; endwhile; else: ?&gt; &lt;?php endif; ?&gt;  &lt;?php $i = 0; rewind_posts(); ?&gt;  &lt;?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query-&gt;next_post(); else : the_post(); ?&gt;  &lt;div id="right-column"&gt;  &lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;   &lt;?php the_field('shop_address'); ?&gt;&lt;br/&gt;   &lt;?php the_field('suburb'); ?&gt;, &lt;?php the_field('postcode'); ?&gt;&lt;br/&gt;   Phone: &lt;?php the_field('phone_number'); ?&gt;&lt;br/&gt;   &lt;a href="&lt;?php the_field('website_address'); ?&gt;"&gt;Website&lt;/a&gt;   &lt;/div&gt; &lt;?php endif; endwhile; else: ?&gt;  &lt;?php endif; ?&gt;
23832
i try to run a custom post type query to match following criteria: sort movies first by year in descending order, after that ("inside" the year order) by title alphabetically.      desired output: movie title A, 2006 movie title Z, 2006 ... movie title A, 1996 movie title Z, 1996    i use the following code:  $wp_query = new WP_Query(); $wp_query-&gt;query( array( 'post_type' =&gt; 'movies',                        'distribution' =&gt; 'companyA',                        'meta_key' =&gt; 'year',                       'orderby' =&gt; 'meta_value_num title',                         'order' =&gt; 'DESC',                        'posts_per_page' =&gt; -1,                        'post_status' =&gt; 'publish', ));   i tried several things but i can only get this "inverse" result:  movie title A, 1996 movie title Z, 1996 ... movie title A, 2006 movie title Z, 2006      if i change DESC, ASC it only changes the title sorting. but i need to apply it to the year and not to the title.  is it the right way to use two orderby values? or do i have to use a meta_query or custom SQL ?   thx in advance!  Here is the resulting SQL Query from $GLOBALS['wp_query']-&gt;request    SELECT&nbsp;wp_posts.* FROM&nbsp;&nbsp;&nbsp;wp_posts &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INNER&nbsp;JOIN&nbsp;wp_term_relationships &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON&nbsp;(&nbsp;wp_posts.id&nbsp;=&nbsp;wp_term_relationships.object_id&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INNER&nbsp;JOIN&nbsp;wp_postmeta &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON&nbsp;(&nbsp;wp_posts.id&nbsp;=&nbsp;wp_postmeta.post_id&nbsp;) WHERE&nbsp;&nbsp;1&nbsp;=&nbsp;1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;(&nbsp;wp_term_relationships.term_taxonomy_id&nbsp;IN&nbsp;(&nbsp;24&nbsp;)&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;wp_posts.post_type&nbsp;=&nbsp;'movies' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;(&nbsp;wp_posts.post_status&nbsp;=&nbsp;'publish'&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AND&nbsp;(&nbsp;wp_postmeta.meta_key&nbsp;=&nbsp;'year'&nbsp;) GROUP&nbsp;&nbsp;BY&nbsp;wp_posts.id ORDER&nbsp;&nbsp;BY&nbsp;wp_postmeta.meta_value&nbsp;+&nbsp;0, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wp_posts.post_title&nbsp;DESC&nbsp;
78286
I got a loop from internet (I forgot the site) for two column post view. It has a nice CSS for it too. But skipping that for the question now.  &lt;?php $counter = 1; ?&gt;                  &lt;?php query_posts( 'cat=3&amp;showposts=6' ); //Find whether the catID is 3 ?&gt;             &lt;?php while ( have_posts() ) : the_post() ?&gt;                   &lt;?php /* Two Column Support get class */ ?&gt;              &lt;?php $class = ( $counter % 2  ? ' one' : '' ) ?&gt;                  &lt;?php /* Two Column output open the div class */ ?&gt;              &lt;div class="column&lt;?php echo $class;// echo $first; ?&gt;"&gt;              &lt;?php news_format(); ?&gt;             &lt;/div&gt; &lt;!-- End Two Column Support close div class --&gt; &lt;?php $counter++; //Update counter ?&gt;   It printed my posts into 2 columns like:  ========= | 1 | 2 | --------- | 3 | 4 | --------- | 5 | 6 | =========  It's fine. But I want to print it like this:  ========= |   1   | --------- | 2 | 3 | --------- | 4 | 5 | --------- | 6 | 7 | =========  What the modification I need to do? I can't pass any custom limit into the WordPress' custom loop &lt;?php while ( have_posts() ) : the_post() ?&gt;. It's for a custom theme. I did it, but the problem is, it's showing like:  ========= |   1   | --------- | 1 | 2 | --------- | 3 | 4 | --------- | 5 | 6 | =========   is my whole bunch of code.
64916
How can I exclude one specific post from a WP_Query query? (For example, show all posts apart from a post with the ID 278)  I've tried the post__not_in argument but it just removes all posts..  Any help would be great.  Here is my current query  &lt;?php     $temp = $wp_query;     $wp_query= null;     $wp_query = new WP_Query(array(         'post_type' =&gt; 'case-study',         'paged' =&gt; $paged,     ));     while ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post(); ?&gt;   Thanks
10789
I'm developing a Custom Post Type plugin, which works fine in dev. Now that I've moved it to production, I'm getting a 404 when I try to display the template for the CPT.   So, two questions:  1- Is there some mechanism to determine the path WP took to get through the hierarchy?  2- Why wouldn't it just give me the single or index page?  I've dumped $wp_query and a stacktrace at the end of the 404. Here's part of what I found:  [query] =&gt; Array     (         [page] =&gt;          [pagename] =&gt; refletters/proximity     )  [request] =&gt; SELECT   wp_posts.* FROM wp_posts  WHERE 1=1  AND (wp_posts.ID = '0') AND wp_posts.post_type = 'page'  ORDER BY wp_posts.post_date DESC  [posts] =&gt; Array     (     )  [queried_object] =&gt;  [queried_object_id] =&gt; 0   And the stack trace:  2013-04-27 22:16:41 - 24.52.197.40 - TRACE - index.php - 404.php[39] -      wordpress/wp-content/themes/notoriousconsultant/404.php[39] - aaLog-&gt;logtrace     wordpress/wp-includes/template-loader.php[50] - include     wordpress/wp-blog-header.php[16] - require_once     wordpress/index.php[17] - require   Thanks for your help.
116534
I have different categories called as reviews, ideas etc. I have a page called as review, I want to display review category to the review page (only review category). Is this possible? Which file I have to change? I have the code below, but it is displaying only the texts not any links to the posts, 7 is my category id.  if( is_page( 'reviews' )){           query_posts("cat=7" );        }