Conditional arguments WP_Query for post custom fields

Here is a modification of your code where you can add the meta query if certain $GET keys are set. $query_array = array(‘relation’ => ‘AND’); if(isset($_GET[‘gender’]) && !empty($_GET[‘gender’])){ $gender = $_GET[‘gender’]; array_push($query_array, array(‘key’ => ‘wpcf-gender’, ‘value’ => $gender, ‘compare’ => ‘=’)); } if(isset($_GET[‘city’]) && !empty($_GET[‘city’])){ $city = $_GET[‘city’]; array_push($query_array, array(‘key’ => ‘wpcf-city’, ‘value’ => $city, … Read more

How can I query posts with newly uploaded images?

This should do it for you. If you’re calling it in a function, you’ll of course need to ensure $wpdb is the global. $required_number_of_attatchments = 3; $posts = $wpdb->get_col( $wpdb->prepare( “SELECT posts.ID FROM %s AS posts INNER JOIN ( SELECT MAX(post_modified) AS modified, post_parent FROM %s WHERE post_type=”attachment” AND post_parent > 0 GROUP BY post_parent … Read more

How can i simulate “taxonomy__in” in query?

You can’t, because of the way categories and general taxonomies operate. Categories are a type of taxonomy, so we are querying one level lower when querying for categories. When you query for category__in => array() it actually looks up what category_terms are queried and queries posts from all those categories. Now this effect we can … Read more

How to apply filter inside a single wp_query?

You should consider using the posts_per_page parameter as suggested by @Tamil. But in general you can also remove the filters you add. In your case you could remove it after your WP_Query() with add_filter( ‘post_limits’, ‘my_post_limits’ ); $slider_query = new WP_Query( $args ) remove_filter( ‘post_limits’, ‘my_post_limits’ ); so it won’t affect later queries. You can … Read more

Next/Prev posts on same page

Pass Paged into parameter array of query_posts You should set get_query_var( ‘paged’ ); if you want your query to work with pagination. $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; $args_news= array( ‘cat’ => 1, ‘posts_per_page’ => 2, ‘orderby’ => ‘post_date’, ‘order’ => ‘DESC’, ‘numberposts’ => -1, ‘paged’ => $paged, ); query_posts( $args_news ); if ( … Read more

Meta Query for specific months

You need the 2 digit month number that you want to query on and then use the code below. This should be easy with php (for example, see this post). In the code below $month is the number of the month in a 2 digit format, eg March would be 03. $start_date = date(‘Y’.$month.’01’); // … Read more

Custom Query – show first post twice?

Just rewind posts after the first post and run the loop again. if($test->have_posts()) : $test->the_post(); the_title(); endif; $test->rewind_posts(); if($test->have_posts()) : while($test->have_posts()) : $test->the_post(); the_title(); endwhile; endif;

WP_query Date Range results not matching loop

Try to run your query with “suppress_filters” => true WP_Query allows for posts to be added after the query has run. found_posts is the actual queried amount of posts, post_count is updated after running the filters. If this fixes it search your code (Any active code, check network activated plugins as well) for the_posts or … Read more