How to get Author ID outside the loop

The simplest and most straightforward way to get the post author ID outside the loop, if you know the post ID, is to use the WordPress core function get_post_field(). $post_author_id = get_post_field( ‘post_author’, $post_id ); If you do not yet know the post ID of the page you are on, then since WP 3.1 the … Read more

Split Content and Gallery

Open to anybody who can simplify this but here’s what I came up with that worked for me. First thing’s first – get the gallery, using get_post_gallery(), as soon as the loop starts: <?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); $gallery = get_post_gallery(); $content = strip_shortcode_gallery( get_the_content() ); ?> <div … Read more

Display featured products through custom loop in woocommerce on template page

Change your args to be like this: $meta_query = WC()->query->get_meta_query(); $meta_query[] = array( ‘key’ => ‘_featured’, ‘value’ => ‘yes’ ); $args = array( ‘post_type’ => ‘product’, ‘stock’ => 1, ‘showposts’ => 6, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘meta_query’ => $meta_query ); If you go to wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@595) you can find how it’s done for … Read more

Why should I put if(have_posts()), is while(have_posts()) not enough?

The WordPress template loader will include the appropriate contextual template file in many circumstances, even if the query for that context returns no posts. For example: The Main Blog Posts Index Category Archive Index (Category exists, but has no posts) Tag Archive Index (Tag exists, but has no posts) Author Archive Index (Author exists, but … Read more

Get post content from outside the loop

You can use get_page() to return the $post object of a static page: $page_id = 302; $page_object = get_page( $page_id ); echo $page_object->post_content; Edit Similarly, you can use get_post() to return the $post object of a post: $post_id = 302; $post_object = get_post( $post_id ); echo $post_object->post_content;

Counting the posts of a custom WordPress loop (WP_Query)?

Correct way of getting the total number of posts is: <?php $count = $custom_posts->found_posts; ?> http://codex.wordpress.org/Class_Reference/WP_Query#Properties Edit: acknowledging @Kresimir Pendic’s answer as probably correct. post_count is the count of posts for that particular page, while found_posts is the count for all available posts that meets the requirements of the query without pagination. Thank you for … Read more