How to get order of posts?

Found the answer from the WordPress docs. For what the query is ordered by, I use: global $wp_query; echo $wp_query->get(‘orderby’); For the order by which the posts are queried: global $wp_query; echo $wp_query->get(‘order’);

get custom post type by tag

You’ll need to setup the post for the query by changing the following line to get rid of the infinite loop. <?php while ($query->have_posts()) : $query->the_post(); ?> If your looking for a custom post type, you’ll need to specify that in the query arguments: <?php $query = new WP_Query( array( “post-type” => “yourposttype”, “tag” => … Read more

Cleanest Way to Select Every Second Element in a Loop?

I wouldn’t consider that strange :). WordPress uses a similar method to apply the ‘alternate’ class to every other row in the tables on the admin page, something like: <ul> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php $class = (empty($class) ? ‘class=”alternate”‘ : ”);?> <li <?php echo $class; ?> ><?php the_content(); ?></li> … Read more

List all posts commented by current user

If we want to avoid any filters and manual SQL queries, we could try (untested): $args = array( ‘post_type’ => ‘foods’, ‘posts_per_page’ => 5, ‘post__in’ => array_unique( wp_list_pluck( get_comments( array( ‘user_id’ => get_current_user_id() ) ), ‘comment_post_ID’ ) ), ); $my_query = new WP_Query( $args );

Display Authors Comments on Profile Page

What you need to use here is the WP_Comment_Query() function. So on the author.php page, you can easily get the author info and ID as followed: // get author info $curauth = (isset($_GET[‘author_name’])) ? get_user_by(‘slug’, $author_name) : get_userdata(intval($author)); // set ID $user_id = $curauth->ID; Then we add the user ID in the query arguments array: … Read more

Using the Loop to show all levels of subpages under a parent page? Halfway there

Sounds like you’re looking for a recursive function, ie a function that calls itself. Here’s a rough outline of how it can be done: function wpse13669_show_all_children( $post_id, $current_level ) { $children = get_posts( array( ‘post_type’ =>’page’, ‘posts_per_page’ =>-1, ‘post_parent’ => $post_id, ‘order_by’ => ‘title’, ‘order’ => ‘ASC’ ) ); if ( empty($children) ) return; echo … Read more

How to return loop contents

There are replacements that return pure strings for all parts, no need to print anything into an output buffer. I like sprintf() and would write your example like this: <?php if ( $cms_pl_pages->have_posts() ) { $content=”<section class=”cms-pl-gallery”>”; while ( $cms_pl_pages->have_posts() ) { $cms_pl_pages->the_post(); $content .= sprintf( ‘<article class=”cms-pl-item clearfix”> %1$s <h2> <a href=”https://wordpress.stackexchange.com/questions/57000/%2$s” title=”Read %3$s”>%4$s</a> … Read more

Display all posts from specific categories on a page

I’d Advise adding the arg of the category in an array. And don’t use query_posts. Also showposts is deprecated use posts_per_page instead. $args = array ( ‘cat’ => array(2,6,9,13), ‘posts_per_page’ => -1, //showposts is deprecated ‘orderby’ => ‘date’ //You can specify more filters to get the data ); $cat_posts = new WP_query($args); if ($cat_posts->have_posts()) : … Read more

Output before and after the loop

The function the_post() (source) triggers the action loop_start when it is first used in the loop. This is used in every WP_Query loop, so you may want to check if the current query is the ‘main’ one (assuming that’s the query you wish to target) add_action( ‘loop_start’, ‘wpse107113_loop_start’ ); function wpse107113_loop_start( $query ){ if( $query->is_main_query() … Read more