More than 1 Year Date Query

You can use before and after, see date arguments. $args = array( ‘posts_per_page’ => ‘-1’, ‘date_query’ => array( array( ‘after’ => ‘December 31st, 2015’, // i.e., 2016+. ‘before’ => ‘January 1st, 2018’, // i.e., before 2018. ), ), ); $query = new WP_Query( $args ); Based on an article written by one of the date … Read more

Apply CSS class to every second database record [closed]

Inside the loop you can use $the_query->current_post to get the index of the post currently being displayed. You can then check if the index is an odd or even number and conditionally add the class. Example: if ( 1 === $the_query->current_post % 2 ) { echo ‘class=”switchable”‘; }

Exhausted memory limit with very simple WP_Query

Your query looks OK, so I doubt that it will cause any problems. But then… Let’s take a look at your loop: if ($mainPosts->have_posts()) { while ($mainPosts->have_posts()) { $postItemImage = the_post_thumbnail_url(); … echo “…”; } } First of all you don’t have any else in there, so there is no point in this if. But … Read more

Php Trying to get property ‘ID’ of non-object

The error says it all. You are trying to get property ID of non-object. That means that the $post var is not an object. You can get current post ID within a WordPress loop using get_the_ID(), but actually you don’t need it in this case (see explanation below). if ( $query->have_posts() ) { while ( … Read more

How do you Query posts with nothing in common?

Maybe you’re looking for the post__in parameter in WP_Query. $query = new WP_Query(array( ‘post__in’ => array(23,18,2,199,6,8) ); And then: while ( $query->have_posts() ) { $query->the_post(); /* post loop */ } Take a look at the docs. =D For public queries: post__in is not public queryable by default, so you can just validate and copy $_GET[‘post__in’] … Read more

Retrieving list of a custom post type in a widget without using WP_Query?

Use wp_reset_postdata() function after while loop to reset custom wp_query as shown in following code so that it will not break other wordpress loop. <?php // Create and run custom loop $custom_posts = new WP_Query(); $custom_posts->query(‘post_type=jobs&posts_per_page=8’); while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?> <li><a href=”https://wordpress.stackexchange.com/questions/98851/<?php the_permalink(); ?>”><?php the_title(); ?></a></li> <?php endwhile; ?> <?php wp_reset_postdata(); ?> For more … Read more