Modifying the Search Results

add_filter( ‘pre_get_posts’, ‘__filter_pre_get_posts’ ); function __filter_pre_get_posts( $query ) { // this is to detect a public search query if ( ! is_admin() && $query->is_search ) { // DO your magic here… } }

Get posts for last working week in WP_Query

Well, try the below code- $base_array = array( ‘posts_per_page’ => -1, ‘fields’ => ‘ids’, ‘post_type’ => ‘cpt’, ‘post_status’ => array(‘publish’), ‘date_query’ => array( ‘after’ => strtotime( ‘previous week Monday’ ), ‘before’ => strtotime( ‘previous week Friday’ ) ) ); $base = get_posts($base_array); I’ve not tested it. But I tested that below code returns the perfect … Read more

WP_Query attachments by multiple IDs

I think you need to use post__in . The code will be something like: $args = array( ‘post__in’ => array( 1, 2 ), ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘no_found_rows’ => true, ‘posts_per_page’ => 2, ‘post_status’ => ‘any’ ); $query = new WP_Query($args);

How can I order Wp_Query hierarchically?

You’ll need to use something other than a single WP_Query. You could use wp_list_pages() with a custom walker to get your desired structure. wp_list_pages in widget, sidebar, or wherever you want the list to appear: <ul class=”cpt-hierarchy”><!– or whatever class you like –> <?php wp_list_pages(array( ‘post_type’ => ‘cpt’, // replace with your cpt’s slug ‘title_li’ … Read more

Pass arguments to WP_Query using “set” method

In the last example (the one that’s not working for you), you’re instantiating an instance of the WP_Query class without passing in an array of query vars. That’s fine, but internally this means the query never actually takes place. You’re calling $q->set( … ), but that’s only setting a query variable, it doesn’t run the … Read more