wp_query with parameters

Assuming you have variables to hold your custom values for each: $category $tag $author $search Then just create an arguments array, and add keys to it conditionally: $custom_query_args = array( // Set any default args here // NOTE: This is where you’d add // things like pagination (posts_per_page), // order/sort parameters, and the like ‘posts_per_page’ … Read more

exclude a post from wp_query loop

If I’m understanding your specific question (and posted code) correctly, you’re actually trying to exclude the most recent post from your query? If so, there’s a much easier method: use the offset parameter: $query2 = new WP_Query( array( ‘offset’ => 1 ) ); Using this method, you exclude the most recent post from $query2, and … Read more

how to invoke wordpress API from other existing PHP system

You can add the following snippet at the start of your PHP file to load WordPress in it. define( ‘WP_USE_THEMES’, false ); require(‘wp-blog-header.php’); // chage the path to match your WP directory //the rest of your code here EDIT: Alternatively, you could use wp-load.php instead of wp-blog-header.php. define( ‘WP_USE_THEMES’, false ); require(‘wp-load.php’); // chage the … Read more

ACF: post query, hide duplicate values [closed]

You could pass all the fields into an array during your while loop, strip the array of duplicates, and then run a foreach loop on that array. It would look something like this: <?php $designers = array(); while ( $the_query->have_posts() ) : $the_query->the_post(); $designers[] = get_field(‘products_page_designer_name’); endwhile; // This will strip out any values that … Read more

How to retrieve the date on which a post was moved to trash?

When post is trashed, WP creates a meta field named _wp_trash_meta_time and it saves trashed time in that field. You can use that field to get trash time. Edit: Example: $args = array( ‘post_status’ => ‘trash’, ‘post_type’ => ‘post’, ‘nopaging’ => TRUE, ‘meta_query’ => array( array( ‘key’ => ‘_wp_trash_meta_time’, ‘value’ => array( strtotime(“last week”), date(‘U’) … Read more

WooCommerce search products between price range using WP_Query

The solution: $query = array( ‘post_status’ => ‘publish’, ‘post_type’ => ‘product’, ‘posts_per_page’ => 10, ‘meta_query’ => array( array( ‘key’ => ‘_price’, ‘value’ => array(50, 100), ‘compare’ => ‘BETWEEN’, ‘type’ => ‘NUMERIC’ ), ), ); $wpquery = WP_Query($query); // return 10 products within the price range 50 – 100

Adding a blog archive with pagination using WP_Query

You can calculate offset via paged and posts_per_page. E.g: $per_page = 6; $paged = get_query_var(‘paged’) ? : 1; $offset = (1 === $paged) ? 0 : (($paged – 1) * $per_page) + (($paged – 1) * 2); $args = array( ‘order’ => ‘ASC’, ‘paged’ => $paged, ‘offset’ => $offset, ‘orderby’ => ‘ID’, ‘post_type’ => ‘post’, … Read more