Trouble inputting variable into WP query

Combining Petar’s function and Tiago’s shortened option syntax, this works: In template: //once, in header: $i = 0; //anytime I want to run the query: $i++; $cat_query = prefix_get_wp_query($i); while ( $cat_query->have_posts() ) { $cat_query->the_post(); get_template_part( ‘template-parts/posts/content’, get_post_type() ); wp_reset_postdata(); } In functions: function prefix_get_wp_query( $i ) { if ( $i > 0 ) { … Read more

Wp-query Order By problem

The “problem” seems to be the default value that order receives, its always DESC unless told otherwise. You will need to create your own order after getting the posts, so something like this. $my_query = new WP_Query($query_args); $ordered_posts = []; foreach(explode(‘,’, $idpaginas) as $rpid) { foreach($my_query->posts as $index => $fpid) { if($fpid->ID == $rpid) $ordered_posts[] … Read more

Loop increase in while loop not working

You’re reassigning 1 to $i on every loop: if ( $i = 1) { This should be if ( $i == 1) { or even if ( $i === 1) { which will also compare the post type, which is best practice. In PHP, = is assignment, == is comparison:

how to get Nearby zipcode

Most likely your values $latitude and/or $longitude are not what you expect them to be. So radians($latitdue) becomes radians() or radians(”). For getting a single row, get_row() is usually the better choice instead of get_results(). $zipdetail = $wpdb->get_row( “SELECT * FROM `li_zipcode` WHERE `zip_code` = ‘$zipcode'”, OBJECT ); If $zipcode is coming from an untrusted … Read more

Can’t fetch Custom Post Type Data through Custom Query

Changing my query to following worked for me. I removed the product ID field & called the post taxonomy directly as I didn’t need the product Id’s. <?php $args = array( ‘post_type’ => ‘colorshades’, ‘fields’ => ‘ids’, ‘posts_per_page’ => -1, ‘meta_query’ => array( array( ‘key’ => ‘colors_product_id’, // ‘value’ => $product->id, // ‘compare’ => ‘=’, … Read more