Use wp_get_recent_posts with search term

It should just be a matter of setting the ‘s’ argument of wp_get_recent_posts() (or just get_posts()) to the search term: $args = array( ‘numberposts’ => $numberposts, ‘offset’ => $offset, ‘category__not_in’ => array(391), ‘category’ => $category, ‘orderby’ => ‘post_date’, ‘order’ => ‘DESC’, ‘post_type’ => ‘post’, ‘post_status’ => ‘publish’, ‘s’ => $searchterm, ); $recent_posts = wp_get_recent_posts( $args, … Read more

WP_Query meta_query >= date

What is missing? One tiny detail… And it’s easy to overlook… the_field(‘datum_event’); Prints the field using the format you’ve defined in field settings. But… It has nothing to do with how the value of that field is stored in DB. ACF uses YYYYMMDD format when storing date values in DB. And WP_Query doesn’t use field … Read more

wp query template tags not working

Template tags are not methods of WP_Query object. They are functions. On the other hand have_posts and the_post are methods of WP_Query. So in your code you should use: while ($test->have_posts()) : $test->the_post(); as you do, but then: echo get_the_title(); echo get_the_content(); Also… if you want to echo these values, it would be much better … Read more

AJAX filter posts by year

Try by change appropriate code: Front end <form action=”<?php echo site_url()?>/wp-admin/admin-ajax.php” method=”POST” id=”filter> <select name=”year”></select> <input type=”hidden” name=”action” value=”filter” /> </form> JS ajax request $.ajax({ url : filter.attr(‘action’) , data : filter.serialize() , type : filter.attr(‘method’) , success : function (response) { console.log(response) }, error : function (request , status , error) { console.log(request.responseText) console.log(error) … Read more

Get all Posts If has same custom field values in Posts

Following code will be proper for the meta query. $query_args = array( ‘post_type’ => ‘service’, ‘posts_per_page’ => -1, ‘meta_query’ => array( array( ‘value’ => $zip, ‘compare’ => ‘LIKE’, ‘key’ => ‘zipcode’, ), ) ); $query = new WP_Query($query_args); <?php if ( $query->have_posts() ) :while ( $query->have_posts() ) : $query->the_post(); ?> <h3><?php the_title(); ?></h3> <?php endwhile; … Read more

How to get the WC_Product Object when using a WP_Query in WooCommerce

The global $product; doesn’t work in a custom WP_Query, instead use wc_get_product() like: if ($slider_products_q->have_posts()): while($slider_products_q->have_posts()): $slider_products_q->the_post(); $product_id = get_the_ID(); $product = wc_get_product($product_id); $price_html = $product->get_price_html() // … Now you can use any WC_Product methods on $product variable. Alternatively, you could use a WC_Product_query.

How do I use `posts_distinct` correctly?

You can see a practical example here: https://wordpress.stackexchange.com/a/142902/19105 Why do you need to use DISTINCT in this situation though? The WP_Query you provided shouldn’t be giving duplicates unless there is a plugin causing trouble. But I’ll try to answer it anyway. I would suggest keeping the function in functions.php. But move the filter to archive.php … Read more