I created a custom post type from a Plugin. Then in a function like show_data() I queried the post type along with its post meta data (Custom Fields). Then in Page Template I used the function to show the queried data. Within the loop I did something like:  while( $my_query-&gt;have_posts() ){    $my_query-&gt;the_post();     echo $post-&gt;ID;     echo get_the_id();     echo get_post_meta( $post-&gt;ID, 'my_field', true );     echo get_the_title();  } wp_reset_postdata();   I did not return anything but simply those echos. And in my Page Template I used simply:  show_data();   Now the problem is: the loop is showing the Post Title (get_the_title()) and the post ID from get_the_id() only.  But the $post-&gt;ID is not showing the ID, so the post_meta is not showing too.  What can cause the problem?
120775
I have the following code and while get_the_ID() works, $post->ID does not, why?      $the_query = new WP_Query( array(         'post_type' =&gt; 'custompost',     ) );      while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();         echo $post-&gt;ID;     endwhile;
12556
Basically on my site I have a bunch of song lyrics and in a static page I want to show them listed by album  and album position of the song and I have two custom fields with that data, sd_album_title and `sd_album_track_n. Plus some of the songs don't have an album yet, so both values are void, and I wish to list those ones at the foot of the others, sorted by alphabetical order.  The latest thing I tried at least to sort everything by the two field is this one:  $key = 'sd_album_title'; $themeta = get_post_meta($post-&gt;ID, $key, TRUE);  add_filter( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) {     if ($themeta != '') {         $query-&gt;set( 'post_type', 'lyric' );         $query-&gt;set( 'meta_key', 'sd_album_title' );         $query-&gt;set( 'orderby', 'meta_value' );         $query-&gt;set( 'order', 'ASC' );         $query-&gt;set( 'meta_query', array(               array(                     'key' =&gt; 'sd_album_title'               ),               array(                     'key' =&gt; 'sd_album_track_n',                     'value' =&gt; 'target_value',                     'compare' =&gt; '&gt;=',                     'type' =&gt; 'numeric'               )         ));     }     return $query; }  $getlist= 'my_get_posts'; $posts = get_posts( $getlist );   And my intention was to put an else at the end of it with an array ordered by title for the other songs, but it returns nothing as it is. Could something like this work with some corrections or am I on a totally wrong path?
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?
81723
Hi i'm using the code below to call post from a certain Category under certain post types, But how do i style it so that i can make the first post different from the rest of the post.  &lt;?php $queryObject = new  Wp_Query( array(         'showposts' =&gt; 4,         'post_type' =&gt; array('pretty-little-liars', 'revenge', 'once-upon-a-time'),         'category_name' =&gt; celebrity,             'orderby' =&gt; 1,         )); // The Loop! if ($queryObject-&gt;have_posts()) { ?&gt;  &lt;?php while ($queryObject-&gt;have_posts()){     $queryObject-&gt;the_post();     ?&gt;  &lt;a href="&lt;?php the_permalink(); ?&gt;" title="&lt;?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?&gt;"&gt;                     &lt;?php the_post_thumbnail('video-post'); ?&gt;                     &lt;/a&gt;  &lt;?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?&gt;                 &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;   &lt;?php } ?&gt; &lt;?php wp_reset_postdata(); } ?&gt;
59096
I found this problem on Wordpress Support and the topic unfortunately is now closed.  I have this same issue... (read below)    We have created a site where members can recommend things like favourite books, movie, songs, etc. For this problem I will use the Movies page as an example.  The "Movies" page is ultimately a custom page template that asks wordpress to display a random list of ALL posts that have been given the category "movies" (category 31). It displays the title of these movies in random order using the code below.  &lt;?php  $rand = new WP_Query("cat=31&amp;showposts=-1&amp;orderby=rand");  while($rand-&gt;have_posts()) : $rand-&gt;the_post(); ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt; &lt;?php endwhile; ?&gt;   Problem is the list is getting rather long and I would like to split it up into two or more pages of about 10 movies each. To achieve this, I have used the code below.  &lt;?php  $page = (get_query_var('paged')) ? get_query_var('paged') : 1;  query_posts("cat=31&amp;orderby=rand&amp;showposts=10&amp;paged=$page");  while ( have_posts() ) : the_post()  ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt; &lt;?php endwhile; ?&gt;   But there is a problem because although it splits up the data into pages of 10 posts each (paginates), it fails to include a new set of 10 posts on page 2 and so forth. In other words, because it is listing things in a random order, it just goes out and gets another 10 random posts (or in this case movie titles). As a result, we have some repeated movie title posts instead of a new set of 10 random movie titles on page 2, etc.  My question is - what can I add to this code to get wordpress to "remember" which random 10 posts it included on page 1, and then have it get a new set of 10 posts to put on pages 2, 3, etc. until all posts are displayed. I would like there to be only one occurrence of a post per page when sorted randomly in sets of 10.
100043
I have a loop within a loop. I figured out how to show all or X number of custom posts after Y number of normal posts. I also figured out how to show 1 custom post after every Y normal posts. How do I show X custom posts after EVERY Y number of normal posts.  In simple terms. How do I show 2 book (custom post type) posts after every 4 blog posts? How does one fetch more than 1 post at a time from the loop?  this is the logic of the x custom posts after y normal posts. how do i get x custom posts after EVERY y normal posts?  main query // normal query $k=0 // setting iterator while have posts, the post  ---post markup---   if($k == 4 or $k % 4 == 0) { // whether after x number or after every x number second query // using WP_Query while second query has posts, the posts  ---custom post type markup // I NEED TO SHOW MORE THAN ONE HERE IF IT IS AFTER EVERY X blogposts  endwhile; //second query ends  }  $k++; endwhile // main query
94872
I have taxonomy color which  is associated with a custom post type. I am listing all the post meta and the taxonomy  in a table. I have a option in the table  to search  the posts matching the search value.   when search key is entered it will do a ajax call to get the the posts.  this is the query to get all the posts matching the search string.  function get_query_posts_custom($post_id,$start,$length)  {     //get_posts arguments     $args =  array(         'post_type'     =&gt; 'custom_post',         'post_status'   =&gt; array('publish'),         'numberposts'   =&gt; $length,         'offset'        =&gt; $start,         'orderby'       =&gt; 'menu_order',         'order'         =&gt; 'asc',         'post_parent'   =&gt; $_product-&gt;id     );       //get custom post taxonomy     $taxonomies = (array) maybe_unserialize(get_post_meta( $post_id, 'post_taxonomy', true));     if(empty($attributes)) {         $taxonomies = array();     }      $meta_keys  = array('type','code','key');            if(!empty($search)) {         foreach($meta_keys as $meta_key) {             $meta_query[] = array(                         'key'   =&gt; $meta_key,                         'value' =&gt; $search,                         'compare'   =&gt; 'LIKE';                     );          }          $args['meta_query'] = $meta_query;         $tax_query = array();          foreach($taxonomies as $taxonomy) {             $tax_query  = array(                 'taxonomy'  =&gt;  $taxonomy;                 'field' =&gt;  'slug';                 'terms' =&gt;  $search;                 'operator'  =&gt; 'LIKE';             );         }         if(count($tax_query)) {             $args['tax_query'] = $tax_query;         }     }     $results    = get_posts($args);             return $results; }   How to get the posts that matches the search string of a taxonomy?  I searched the wordpress function reference  it says only operator allowed to tax_query are 'IN,NOT IN, OR and AND')  can I use the LIKE operator?
84610
Is there any way I can take a URL of an image and find the attachment or post id of that image in the database?  Here is the situation:  I'm in a loop going over all the 'img' tags that are surrounded by 'a' tags in my post content. if the src attribute of the 'img' tag does not match the href attribute of the outer 'a' tag, then i want to replace the 'img' tag. In doing this, if the 'img' that is to be removed is in the gallery, i want to delete that post, and then put my replacement 'img' in its place. I tried using a function like this:   function find_image_post_id($url) {   global $wpdb;   $postid = $wpdb-&gt;get_var($wpdb-&gt;prepare("SELECT DISTINCT ID FROM $wpdb-&gt;posts WHERE guid='$url'"));   if ($postid) {     return $postid;   }   return false; }   This apparently is not right because the guid is ironically not globally unique. I had (earlier in the same script) uploaded a file with the same name (why? because it was higher resolution and i am trying to replace low resolution versions of the same image) and although wordpress will save the image with a different name in the directory, the guid's were set to be the same. (possibly a bug).  Is there another technique I can use?
30177
i am getting a weird error - white screen in the list of posts for a specific Custom post type (just for that one)   tried deactivating all the plugins tried checking for error (debugging = true)   Still nothing the page just doesnt echos anything... (nothing in source too)  I am talking about such a url in the admin:     Here is register_post_type part i am using:    function register_submodelcpt() {     $labels = array(         'name'                  =&gt; __('Sub Models', THEME_NAME),         'singular_name'         =&gt; __('Sub Models', THEME_NAME),         'add_new'               =&gt; __('New Model', THEME_NAME),         'add_new_item'          =&gt; __('Add new Model', THEME_NAME),         'edit_item'             =&gt; __('Edit Model', THEME_NAME),         'new_item'              =&gt; __('New Model', THEME_NAME),         'all_items'             =&gt; __('All Sub Models', THEME_NAME),         'view_item'             =&gt; __('Watch Model', THEME_NAME),         'search_items'          =&gt; __('Search Models', THEME_NAME),         'not_found'             =&gt;  __('No Models found', THEME_NAME),         'not_found_in_trash'    =&gt; __('No Models found in trash', THEME_NAME),          'parent_item_colon'     =&gt; '',         'menu_name'             =&gt; __('Sub Models', THEME_NAME),      );      $args = array(         'labels'                =&gt; $labels,         'public'                =&gt; true,         'publicly_queryable'    =&gt; true,         'show_ui'               =&gt; true,          'show_in_menu'          =&gt; true,          'query_var'             =&gt; true,         'rewrite'               =&gt; array('slug' =&gt; 'submodels'),         'capability_type'       =&gt; 'post',         'has_archive'           =&gt; true,          'hierarchical'          =&gt; true,         'menu_position'         =&gt; 5,         'menu_icon'             =&gt; get_stylesheet_directory_uri().'/images/cpt/subcars.png',                     'supports'              =&gt; array('title', 'thumbnail', 'revisions', 'page-attributes')     );      register_post_type('submodelscpt',$args); } add_action('init', 'register_submodelcpt');   Did anyone encounter such an issuse? can you think of a reason this might happen?   Another weird thing when i change this:    To this:     The posts list loads correctly...
43594
I want to display custom type posts from a custom taxonomy category so I did this: $news = get_posts( array(     'posts_per_page' =&gt; -1,     'post_type' =&gt; 'news',     'tax_query' =&gt; array(             'taxonomy' =&gt; 'news_category',             'field' =&gt; 'term_id',             'terms' =&gt; 82,         ),     ) );  $postslist = get_posts( $news );      foreach ($postslist as $crap) { ?&gt;      &lt;a href=&quot;&lt;?php $crap-&gt;title; ?&gt;&quot;&gt;&lt;?php $crap-&gt;title; ?&gt;&lt;/a&gt; &lt;?php }?&gt;  I get nothing from this. Why is that? I double checked the category ID and its correct. Also, I used the Custom post types plugin to register the custom taxonomy.
72968
I am attempting to create an AJAX call to pass custom taxonomy into my WP_Query but I am getting 400 (Bad Request). I believe my data array is built correctly so I am not sure what's causing it. Does anything look off here? Is there a way for me to get a more detailed error of why it is returning error 400?  $.ajax({     type:'POST',     url:ajaxUrl,     data: {         taxonomy: 'products',         slug: 'shirts'     },     beforeSend:function(xhr){      },     success:function(data){       $('#response').html(data); // insert data     } });   I console logged ajaxUrl and I am getting the correct path to admin-ajax.php so I know that's not the issue. I think it has to have something to do with data.
65638
The incomplete , says very simply:  rewind_posts(): Rewind the loop posts.  As per , with 's answer, I got: &lt;?php // fetch first post from the loop the_post();  // get post type $post_type = get_post_type();   // rewind the loop posts rewind_posts(); ?&gt;  With Ian Stewart's theme development tutorial, I found rewind_posts()'s use in archive.php, category.php, tag.php, author.php: &lt;?php the_post(); ?&gt; &lt;!-- echo page title --&gt; &lt;?php rewind_posts(); ?&gt; &lt;?php while ( have_posts() ) : the_post(); ?&gt;    &lt;!-- echo content --&gt; &lt;?php endwhile; ?&gt;  But in TwentyThirteen theme we can't see something like this, but a simple WordPress loop with conditional: &lt;?php if ( have_posts() ) : ?&gt; &lt;!-- echo page title --&gt; &lt;?php while ( have_posts() ) : the_post(); ?&gt;    &lt;!-- echo content --&gt; &lt;?php endwhile; ?&gt; &lt;?php endif; ?&gt;  So, I just want to know, while I have the WordPress loop to use, and that works with pagination also, then where do I need to REWIND THE LOOP, and why? EDIT Ok, after the first answer, I got a very good article describing the 3 Query-reset functions in WordPress:  »  by Jeff Starr - DigWP.com  I hope with this the answer can be a lot more educative than currently what we got.
9294
I am building a custom WordPress function/plugin that uses get_posts.  The following code returns an empy array.  I have included wp-load.php in my script.  Is there something else I am missing?    $args = array(     "posts_per_page"   =&gt; -1,     "paged"            =&gt; 0,     "orderby"          =&gt; "post_date",     "order"            =&gt; "DESC",     "post_type"        =&gt; "carellcars, ccf, attachment",     "post_status"      =&gt; "publish, inherit",     "post_author"       =&gt; "2"  );  $posts_array = get_posts($args);   print_r($posts_array);  die;   If I use arrays for post_type and _status the array is not populated, but if I use single values for post_type and _status the array is populated?  Not populated:  $args2 = array(     'posts_per_page'   =&gt; -1,     'paged'            =&gt; 0,     'orderby'          =&gt; 'post_date',     'order'            =&gt; 'DESC',     'post_type' =&gt; array('carellcars', 'ccf', 'attachment'),     'post_status'      =&gt; array('publish', 'inherit') );   Populated:  $args2 = array(     'posts_per_page'   =&gt; -1,     'paged'            =&gt; 0,     'orderby'          =&gt; 'post_date',     'order'            =&gt; 'DESC',     'post_type' =&gt;  'carellcars',     'post_status'      =&gt; 'publish' );   Update, removing "ccf" post type produces results, but I do have a ccf post_type and would like to include them.  Any ideas?      'post_type' =&gt; array('carellcars', 'attachment'),     'post_status'      =&gt; array('publish', 'inherit')
34267
Is there a away to create a custom post type in wordpress that will only include the default metaboxes  and not any other metabox that was added by other plugins.   The idea is to use the custom post to develop a plugin, this plugin will need to have all the functions custom post type but with all the metaboxes that plugin add like all-in-seo.  I want to create a clean interface for the user and those extra metaboxes are in the way :-)
68435
What I need is some function to echo file name of image used in post like "image-file-name.jpg", or just "image-file-name".  Also it would be great to be able to have even more information about image like:   image width,  image height,  image mime type / format,  image file size,  image extension.   Also this may be helpful, I have custom field named 'image_full_url' which contains full url address of image used in post. For example content of custom field named 'image_full_url' is:  http://www.example.com/wp-content/uploads/2012/12/image-file-name.jpg   I am using that custom field to echo that URL when I need it, and it would be nice to have something similar to echo other data about image.
90821
If I have a loop running from a category query like :  &lt;?php $the_query = new WP_Query('cat=1&amp;showposts=50&amp;orderby=title&amp;order=asc');?&gt; &lt;ul&gt; &lt;?php while ($the_query-&gt;have_posts()) : $the_query-&gt;the_post();?&gt; &lt;li&gt;.. &lt;/li&gt;&lt;?php wp_reset_query(); ?&gt; &lt;?php endwhile; ?&gt; &lt;/ul&gt;   How would I create an if clause that breaks the list at a certain interval, and starts a new one. So for example at the 10th post, return a &lt;/ul&gt; and start a new one &lt;ul&gt; at 11.  This is incorrect but to illustrate my objective:  &lt;?php $count =0;     while($count &lt;=50){         if ($count == 9){             echo "&lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;                       &lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;";             }          elseif ($count == 10){         echo "&lt;ul&gt;&lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;                           &lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;";         }         else {         echo "&lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;";         }   What is the correct way to include this logic into the loop?
64728
I have a loop where I display all posts on the site, the problem is that the_excerpt is not being displayed. What am I doing wrong?    &lt;?php      $all_posts = get_posts(array('numberposts' =&gt; -1));     $total_posts = count($all_posts);     $posts_per_column = intval($total_posts / 3);     $count = 0;     $col = 1;     foreach($all_posts as $post):   ?&gt;    &lt;!--post--&gt;   &lt;div class='post' id='article&lt;?php echo $count; ?&gt;'&gt;     &lt;div class='thumb'&gt;        &lt;a href='&lt;?php the_permalink() ?&gt;'&gt;       &lt;!--todo: round thumb images and make them of variable height--&gt;       &lt;?php the_post_thumbnail() ?&gt;       &lt;?php          $cat = get_the_category();          $cat_long = $cat[0]-&gt;cat_name;         $cat_split = explode(' ', $cat_long);          $cat_short = strtolower($cat_split[0]);       ?&gt;       &lt;/a&gt;       &lt;span class='tag &lt;?php echo $cat_short ?&gt;'&gt;&lt;?php echo $cat_long ?&gt;&lt;/span&gt;     &lt;/div&gt;      &lt;div class='inner &lt;?php echo $cat_short ?&gt;'&gt;       &lt;h2 class='entry-title'&gt;&lt;a href='&lt;?php the_permalink() ?&gt;'&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;&lt;/h2&gt;     &lt;/div&gt;     &lt;div class='entry-content'&gt;       &lt;?php the_excerpt() ?&gt;     &lt;/div&gt;   &lt;/div&gt;   &lt;!--end post--&gt;