How to use WP_Query without breaking the main Query

it’s unclear when to use one or the other, or if they must be used at all If you’ve modified the global $wp_query variable (e.g. using query_posts() which should NOT actually be used), then use wp_reset_query() to restore the variable back to the main query, i.e. so that the variable references or points to the … Read more

Pagination of a WP_Query Loop in a child-page page template

Glad that you managed to figure out a solution, which yes, does work. But you could make it more selectively, i.e. target just the specific pages, by using is_single( ‘<parent slug>/<child slug>’ ), e.g. is_single( ‘family-law/success-stories’ ) in your case. Secondly, it’s not exactly the redirect_canonical hook which caused the (301) redirect on singular paginated … Read more

How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?

You could do something like this add_action(‘pre_get_posts’, ‘bt_search_posts’); function bt_search_posts ($query) { // the posts ids that contain foo bar OR lorem ipsum dolor OR ping pong $posts_ids = []; // get the current query vars $query_vars = $query->query_vars; // loop each term we want to search and if found, add the post id into … Read more

Query with a meta value inside a given range

In this situation you will want to use the meta_query parameter, an example: $args = array( ‘post_type’ => ‘page’, ‘post_status’ => ‘publish’, ‘meta_query’ => array( array( ‘key’ => $key, ‘value’ => array($min, $max), ‘compare’ => ‘BETWEEN’, ), ), ); $query = new WP_Query( $args ); The possible values for for the ‘compare’ key are: “=” … Read more

Get posts for which a custom field is not present, with get_posts

You could use NOT EXISTS as the value for comparem, you could also check for empty value if thats relevant This code only checks if hidden meta does not exists $postsForSitemap = get_posts([ ‘numberposts’ => -1, ‘orderby’ => ‘modified’, ‘post_type’ => [‘post’, ‘page’], ‘order’ => ‘DESC’, ‘meta_key’ => ‘hidden’, ‘meta_compare’ => ‘NOT EXISTS’ ]; This … Read more

How To Fix WP Query Returns Results But Shouldn’t?

Couple of things to consider here: 1.) Try adding the meta query only if the value is present: $pairedThing = get_field(‘myThing’); $args = [ ‘posts_per_page’ => 2, ‘post_type’ => ‘thing’, ]; if ( ! empty( $pairedThing ) ) { $args[‘meta_key’] = ‘identifier’; $args[‘meta_value’] = $pairedThing; } 2.) Account for posts without the meta-key set: $pairedThing … Read more

Why is variable not working on custom sql query using wpdb?

Huge thanks to @kero !! I was finally able to create a solution by making the sql statement a variable first (as kero suggested) then using that variable on wpdb->query(); Here’s my updated and fully working codes now: $imdbid = $_GET[‘id’]; $string = file_get_contents(“http://www.omdbapi.com/?i=”.$imdbid.”&apikey=9a187152″); $json = json_decode($string, true); if( $json[‘Response’] === “True” ){ $dataID = … Read more