WP_Query using meta_query returns 1 record
-
Hi,
I’ve been running into an issue where my WP_Query, using a meta value to filter, is only returning 1 record, the filters are matching correctly but it seems to be limited to a single record. I’ve set post_per_page => -1
Here are some attempts I’ve made at running the Query from some other examples I’ve found on the forums;
Attempt 1
$meta_query[] = array( 'key' => 'product', 'value' => $_POST['searchField'], // This is OK, WP_Query will sanitize input! 'compare' => 'LIKE', ); $args = array( 'post_type' => 'is-allowed', 'posts_per_page' => -1, 'orderby' => 'meta_value_num', 'meta_key' => 'product', //'order' => 'ASC', 'meta_query' => $meta_query ); $posts = new WP_Query( $args );
Attempt 2
$params=array( 'posts_per_page' => -1, 'post_type' => 'is-allowed', 'meta_key'=>'product', 'meta_value'=>$_POST['searchField'], 'meta_compare' => 'LIKE', ); $query = new WP_Query; $posts = $query->query($params);
Attempt 3
$args = array( 'post_type' => 'is-allowed', 'meta_query' => array( array( 'key' => 'product', 'value' => $_POST['searchField'], 'compare' => 'LIKE', ), ), ); $posts = new WP_Query( $args );
In the likely case I’m doing something wrong with the php for generating teh table rows, i’ll post that here as well
foreach ( $posts as $post) { if (get_post_meta( $post->ID, 'product', true) <> '') { echo '<tr>'; echo '<td width="20%">' . get_post_meta( $post->ID, 'product', true) . '</td>'; echo '<td width="10%">' . get_post_meta( $post->ID, 'info-line-1', true). '</td>'; echo '<td width="10%">' . get_post_meta( $post->ID, 'info-line-2', true). '</td>'; echo '<td width="10%">' . get_post_meta( $post->ID, 'info-line-3', true). '</td>'; echo '<td width="50%">' . get_post_meta( $post->ID, 'info-line-4', true) . '</td>'; echo'</tr>'; } }
This is probably related, but i had to add a check of empty fields otherwise i was getting multiple blank rows (unfortunately it didn’t add up to the number of missing records)
Sorry if I’ve missed anything, and for my sloppy code. It’s been a decade since I last did any php and this ended up falling in my lap when I walked into a meeting and said, ‘Hey, been a while since I’ve seen php script’ which made me the most knowledgeable person in the room.
- The topic ‘WP_Query using meta_query returns 1 record’ is closed to new replies.