Loop increase number

You can add a php variable and increment it every time the while loop runs $args = array( ‘post_type’ => ‘events’, ‘order’ => ‘ASC’, ); $wp_query = new WP_Query($args); ?> <script type=”application/ld+json”> { “@context”:”http://schema.org”, “@type”:”ItemList”, “itemListElement”:[ <?php $i=0; while ( have_posts() ) : the_post(); $i++; ?> { “@type”:”ListItem”, “position”:”<?php echo $i; ?>”, “url”:”<?php the_permalink(); ?>” … Read more

Display an image if odd number of posts in grid

The common mistake people make in this situation is they start to introduce bunch of custom counters to keep track of state. Really WP_Query just has those already. In your case that would be global $wp_query and $wp_query->post_count and $wp_query->current_post. That should be quite enough to figure out even/odd overall and current iteration in the … Read more

Custom Pagination is Broken

Chances are your pager is getting the count of all posts, regardless of whether they are published. In your query, you should add a filter for only published posts. $query_args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => 4, ‘paged’ => $paged, ‘post_status’ = ‘publish’, ); Then the results count and number of pager links should … Read more

in Foreach Loop the Description is not showing?

Please read about the get_the_permalink and get_the_title. These functions are developed to fetch the details from global $post variable. Your local $post won’t be effective here. You should either pass local $post to these functions like get_the_permalink($post) and get_the_title($post). For get_the_content, you will need to use apply_filters(‘the_content’,$post->post_content); However, preferred way is to create an object … Read more

WP Query – order posts by meta field first and then order the rest

You should be able to name and sort your meta query clauses like this: $meta_query = array( ‘relation’ => ‘OR’, ‘foo’ => array( ‘key’ => ‘expiring_date’, ‘value’ => date(‘Ymd’), ‘compare’ => ‘>’, ‘type’ => ‘DATE’ ), ‘bar’ => array( ‘key’ => ‘expiring_date’, ‘compare’ => ‘NOT EXISTS’ ), ‘baz’ => array( ‘key’ => ‘expiring_date’, ‘value’ => … Read more

Main query not querying any posts in custom taxonomy template

I think the issue here is that when registering the post type, you’ve set exclude_from_search to true. See this note from the WordPress Coedex: Note: If you want to show the posts’s list that are associated to taxonomy’s terms, you must set exclude_from_search to false (ie : for call site_domaine/?taxonomy_slug=term_slug or site_domaine/taxonomy_slug/term_slug). If you set … Read more

Conditional: IF current user has NOT commented on current post (not including post author)

[EDIT] Here is the modified code: $post_id = get_the_ID(); // ID of the current post. $user_id = get_current_user_id(); // ID of the current user. $post_author_id = get_post_field( ‘post_author’, $post_id ); $is_post_author = ( $user_id && $post_author_id == $user_id ); $can_comment = $is_post_author; /* * If $can_comment is not yet TRUE, then it’s because either: * … Read more

get_template_part causes 500 error

The get_template_part() function takes filepath relative to the file it is called in. Do you have a folder called ‘content’ which contains a file content-posttype.php? is the ‘content’ folder located in the same folder as this php file you are calling get_template_part() in? Also, you can modify your while loop as follows: while ( $postsQuery->have_posts() … Read more