wordpress loop for specific category

Pretty sure query_posts is the worst way to query… Always use get_posts from what I’m constantly told. Remove the arguments that you don’t use on the array below. $args = array( ‘posts_per_page’ => 5000, ‘offset’ => 0, ‘category’ => , ‘orderby’ => ‘post_date’, ‘order’ => ‘ASC’, ‘include’ => , ‘exclude’ => , ‘meta_key’ => , … Read more

the_title(); works in a page template, outside the loop. Why?

The global $post object exists already for singular views before wp_head is called. It is just not filled with all data. The the_title() calls get_the_title() which in turn calls get_post(). And that calls $GLOBALS[‘post’] if no post ID has been passed. See also Generating the ogp tags in theme for a use case.

Displaying post per day

This is quite basic but should help you on your way. //Get posts for the current week $args = array( ‘date_query’ => array( array( ‘year’ => date( ‘Y’ ), ‘week’ => date( ‘W’ ), ) ) ); //Check for search query if ( isset( $_GET[‘s’] ) ) { $args[‘s’] = $_GET[‘s’]; } //Create these variables … Read more

When to use wp_reset_postdata();

First we should talk about why you need to use wp_reset_query() in the first place. What wp_reset_query() does is reset the global $wp_query object back to it’s original state. Since you’re creating a new WP_Query object / instance, it does not overwrite the global $wp_query object. The only real case you would need to call … Read more

Why is it necessary to call rewind_posts() when using the loop more than once? [duplicate]

Global rewind_posts File: wp-includes/query.php 784: /** 785: * Rewind the loop posts. 786: * 787: * @since 1.5.0 788: * 789: * @global WP_Query $wp_query Global WP_Query instance. 790: */ 791: function rewind_posts() { 792: global $wp_query; 793: $wp_query->rewind_posts(); 794: } and rewind_posts from WP_Query class. File: wp-includes/class-wp-query.php 3144: * Rewind the posts and reset … Read more

Display subpages under parent page as a list within a loop

First set your arguments(settings) $args = array( ‘post_parent’ => get_the_ID(), ‘post_type’ => ‘page’, ‘numberposts’ => -1, ‘post_status’ => ‘publish’ ); $children = get_children( $args, $output ); then you can use something like this <?php if (!empty($children)):?> <ul class=”row”> <?php foreach($children as $dest){ $permalink = get_permalink($dest->ID); echo “<li class=”col-sm-4″><a href=”https://wordpress.stackexchange.com/questions/254608/{$permalink}”>” . $dest->post_title . “</li>”; }?> </ul> … Read more

Loop with Dynamic Categories

The code below enables categories for pages. An example page template is provided which loops through the categories assigned to the page and displays the posts for each category. If you want to limit the user to selecting only one category, you can use a solution such as Taxonomy Single Term. Associate the category taxonomy … Read more