Loop of custom post type names

I don’t think there’s a way, in WP’s native features, to give a post type, itself, meta. You can get post types in your code using get_post_types( $args, $output, $operator ). $post_types = get_post_types( $args, $output, $operator ); https://codex.wordpress.org/Function_Reference/get_post_types But the only way I can think of to exclude post types based on a mutual … Read more

Loop repeating design pattern

You could use 3 separate loops, and break out of each loop early once you’ve got enough posts. For example, here is a 2 column grid with a single query: $q = new WP_Query( … ); if ( $q->have_posts() ) { ?> <div class=”columns”> <div> <?php $counter = 0; while( $q->have_posts() ) { $q->the_post(); the_title(); … Read more

Adding shortcode closing tag after a loop

Collect all of the loop’s output in a variable, wrap it in the accordion shortcode, then pass that through do_shortcode: $output=””; if ( have_posts() ) : while ( have_posts() ) : the_post(); $output .= ‘<div class=”col-lg-12 col-md-12 col-sm-12 col-xs-12″>’; $output .= do_shortcode(‘[su_spoiler title=”‘.get_the_title().'” open=”no” style=”default” icon=”plus” anchor=”” class=””]’.get_the_content().'[/su_spoiler]’); $output .= ‘</div>’; endwhile; else : $output … Read more

Loop posts only excluding first post

You don’t need separate queries, you can run multiple loops on the same query- // output first post if( have_posts() ){ the_post(); the_title(); } // output the rest of the posts… if( have_posts() ){ while( have_posts() ){ the_post(); the_title(); } } You can also use rewind_posts() to reset the current post back to 0, as … Read more

How can i display post loop in table format?

You need 3 separate loops. One for each row, with each post creating a new column: <div class=”acadp”> <table> <tr> <?php while( $acadp_query->have_posts() ) : $acadp_query->the_post(); ?> <td><?php the_title(); ?></td> <?php endwhile; ?> </tr> <tr> <?php while( $acadp_query->have_posts() ) : $acadp_query->the_post(); ?> <td><?php the_content(); ?></td> <?php endwhile; ?> </tr> <tr> <?php while( $acadp_query->have_posts() ) : … Read more

wp_query to find posts by year and month

Solved by creating custom sql queries. Request for years: SELECT count(*), $wpdb->posts.id as post_id_ts, YEAR( (SELECT $wpdb->postmeta.meta_value FROM $wpdb->postmeta WHERE $wpdb->postmeta.post_id = post_id_ts AND $wpdb->postmeta.meta_key = ‘docs_pubdate’)) AS years FROM $wpdb->posts WHERE $wpdb->posts.post_type=”document_base” AND $wpdb->posts.post_status=”publish” GROUP BY years which will return result like this: Array ( [0] => Array ( [count(*)] => 1 [post_id_ts] => … Read more

Way to display “Yesterday”, “Today”

It took me less than a minute of asking the googles (or bing, or duck) to find this answer: https://stackoverflow.com/questions/3454258/php-date-yesterday-today The accepted answer has the code you want to use (or will give you a starting point). (And this is not really a WordPress question…it’s a PHP question. PHP questions belong on the Stack Overflow … Read more

Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX

Because of this line: $paged = get_query_var( “paged” ) ? get_query_var( “paged” ) : 1; This is inside a function that is used on both an AJAX handler, and on a page request. get_query_var pulls the parameters from the main query ( aka the primary WP_Query that powers functions such as the_post, have_posts() etc ). … Read more

Pagination gives extra page with no results

I am not sure, but I have a guess that it is because you are using pagination and offset at the same time. Pagination might be calculated for whole set, but you are reducing set size with offset so number of pages becomes overestimated.

While in “the loop”, detect if a post is the most recent

In addition to @Milo Answer (this avoids a senseless query, because we already got every needed information from the current wp_query): if ( have_posts() ) : while ( have_posts() ) : the_post(); $headline_html_tag = $GLOBALS[‘wp_query’]->current_post === (int) 0 && $GLOBALS[‘paged’] === (int) 1 ? ‘1’ : ‘2’; the_title( ‘<h’.$headline_html_tag.’><a href=”‘.get_permalink().'” title=”‘.the_title_attribute( array( ‘before’ => ‘Permalink … Read more