I'm clearly not doing this right. Any help would be greatly appreciated. Essentially i am pulling nearby zip codes and city names to a search that is being done. i then want to use WP_Query to pull the WordPress post types matching those zip codes and city names. I'm not super familiar with WordPress, so it is highly likely i'm way off base on how to do this:
$url = "http://www.zipcodeapi.com/rest/OeAp3k78myEhBy0oqSlQSlUWOt6N7TjW8Tlbdtkz1YRCwS1WKmNDIHzwbFjizCeI/radius.json/" . $searchbox . "/100/km";
$response = file_get_contents($url);
$json = json_decode($response);
$post_type = 'location';
$citysearcharray = array();
$zipsearcharray = array();
$searcharray = array();
foreach($json->zip_codes as $nearbyzip)
{
$citysearcharray[] = array(
'key' => 'city',
'value' => $nearbyzip->city,
'compare' => '='
);
$zipsearcharray[] = array(
'key' => 'zip_code',
'value' => $nearbyzip->zip_code,
'compare' => '='
);
}
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'caller_get_posts'=> 1,
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
$citysearcharray,
$zipsearcharray));
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$count=0;
echo '<ul class="location">';
while ($my_query->have_posts()) : $my_query->the_post();
...
Amy i putting the $args together correctly?
TIA
In your foreach, instead of adding new full values to your arrays
$zipsearcharray
and
$citysearcharray
have the value key as an array so:
'value' => array(array of cities)
then change
compare => 'IN'
Related
I'm trying to filter a Wordpress query based on terms in a custom taxonomy. I'm using the Essential Grid plugin to show posts from a custom post type 'offer'.
I'd like to grab values from the 'f' URL parameter and then use those values as the terms for filtering the query.
I'm not sure why my code isn't working. When I try to save it, I get a message that it will cause a fatal error.
Can someone with more PHP experience help me out? I've gone through the code multiple times and haven't seen the issue. Tested several variations as well.
function eg_mod_query($query, $grid_id){
if($grid_id == 3) {
$f = $_GET['f'];
$filters = explode(', ', $f);
$args = array(
'post_type' => 'offer',
'tax_query' => array(
array(
'taxonomy' => 'offer_taxonomy',
'field' => 'slug',
'terms' => array($filters),
),
),
);
$query = new WP_Query( $args );
}
return $query;
}
$filters = explode(', ', $f);
Line above is already in an array format. You should not change that to
array($filters).
In args var which changes the format.
I think this should work:
function eg_mod_query($query, $grid_id){
if($grid_id == 3) {
$f = $_GET['f'];
$filters = explode(', ', $f);
$args = array(
'post_type' => 'offer',
'tax_query' => array(
array(
'taxonomy' => 'offer_taxonomy',
'field' => 'slug',
'terms' => $filters,
),
),
);
$query = new WP_Query( $args );
}
return $query;
}
Thanks.
my first query is okay
$ids = [];
$novidades = get_posts( array(
'posts_per_page' => 4,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
if ( count( $novidades ) ) {
foreach( $novidades as $novidade ) {
$ids[] = $novidade->ID;
}
}
//rest of my code is ok
but, i try post another post and ignore the first query, but don't work, list all post
$args2 = array(
'post_type' => 'post',
'posts__not_in' => $ids
);
$featured = new WP_Query($args2);
Can help me?
It's post__not_in. Remove the extra s from your code.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Your code should be:
$args2 = array(
'post_type' => 'post',
'post__not_in' => $ids,//<====extra 's' removed
);
$featured = new WP_Query($args2);
I have an array of Wordpress page parent IDs which I then use a FOREACH loop to replace each parent ID with the name of the page (parent).
The problem is the array is sorted by the ID order, and I Want to now re-order it using the titles.
How can I do this?
function bv_quick_select_get_category_options() {
$procedure_pages = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-procedure-minimal.php'
)
)
));
$procedure_categories = array();
foreach($procedure_pages as $p1) {
array_push($procedure_categories, wp_get_post_parent_id($p1->ID));
}
$output = '';
foreach(array_unique($procedure_categories) as $c) {
$output .= '<option value="'.get_post($c)->post_title.'">'.get_post($c)->post_title.'</option>';
}
return $output;
}
I would think it would be more efficient to fetch the categories with one query based on the ids, and do the sorting by title in the database. This is my best attempt at how to do that from a quick read of the wordpress docs, but I'm not a wordpress user so it probably needs some work.
function bv_quick_select_get_category_options() {
$procedure_pages = get_posts(array(
'post_type' => 'page',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-procedure-minimal.php'
)
)
));
$ids = array_column($procedure_pages, 'ID');
$categories = get_posts([
'include' => $ids,
'order_by' => 'title'
]);
$output = '';
foreach($categories as $c) {
$output .= '<option value="'.$c->post_title.'">'.$c->post_title.'</option>';
}
return $output;
}
I'm using the Posts 2 Posts plugin. I have many different post types and many different connection types.
I create all those relationships like so
function register_post_type_connections() {
$connection_post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'government');
foreach($connection_post_types as $post_type){
p2p_register_connection_type( array(
'name' => $post_type .'_to_structure',
'from' => $post_type, // use $my_post_types if you didn't define $temp_array
'to' => 'structure',
'reciprocal' => false,
'duplicate_connections' => true,
'sortable' => true,
));
}
// Foreach repeated for each $connection_post_types
}
add_action( 'p2p_init', 'register_post_type_connections' );
The foreach loops is repeated 7 total times in this function to get every possible combination. I've tested the result and it works correctly
Many connections are made and attached to individual post pags. I want to show a list of all these connections made.
I get a complete list of all the connection types like this
function get_all_connection_types() {
$connection_types = array();
$connection_post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'government');
foreach($connection_post_types as $post_type){
$connection_types[] = $post_type .'_to_person';
$connection_types[] = $post_type .'_to_nonprofit';
$connection_types[] = $post_type .'_to_business';
$connection_types[] = $post_type .'_to_article';
$connection_types[] = $post_type .'_to_event';
$connection_types[] = $post_type .'_to_structure';
$connection_types[] = $post_type .'_to_government';
}
return $connection_types;
}
Then I run my loop
$post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'timeline', 'government');
$connection_types = get_all_connection_types();
$connected = new WP_Query( array(
'connected_type' => $connection_types,
'post_type' => $post_types,
'connected_items' => 'any',
'connected_direction' => 'to',
'posts_per_page' => 20,
'orderby' => 'meta_value',
'connected_orderby' => 'date',
'connected_order' => 'desc'
) );
echo '<ul>';
if ( $connected->have_posts() ) {
while ( $connected->have_posts() ) :
$connected->the_post();
Here are my connections from the database
My loops is only returning person_to_person connections. So I tested by changing connected_type to..
$connection_types = array('person_to_structure', 'person_to_person');
This gives me the person_to_structure connections but not the person_to_person.
Why?
You need to pass "connected_direction" as array with exact length of that "connected_type" has for this change this code on your index file.
$post_types = array('person', 'nonprofit', 'business', 'article', 'event', 'structure', 'timeline', 'government');
$connection_types = get_all_connection_types();
$direction_array = array();
for($i=0;$i<count($connection_types);$i++) {
$direction_array[$i] = 'from'; // if you want then you can send the from as well;
}
$connected = new WP_Query( array(
'connected_type' => $connection_types,
'post_type' => $post_types,
'connected_items' => 'any',
'connected_direction' => $direction_array,
'posts_per_page' => 20,
'orderby' => 'meta_value',
'connected_orderby' => 'date',
'connected_order' => 'desc'
) );
This will work.
I am needing some assistance. My goal is to query "clinics" post type by array of zip codes. Then get the ID's of those clinics and run another query of post type called "clinicspromo" to get those results. Then inside the loop, run query #3 to retrieve the clinic information again that is tied to each clinic promo. I have most of it completed, I am just having a problem turning the results of $post->ID; to an array of ID's separated by commas just like the zip code list. Any help would be appreciated!
$zipcodelist = '90001, 90002, 90003';
$args = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );
$postlist = get_posts( $args );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$argstwo = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinicpromos',
'meta_query' => array (
array (
'meta_key' => 'assignclinic',
'meta_value' => $current,
'compare' => 'IN'
)
) );
$the_query = new WP_Query( $argstwo );
Changed your foreach loop like: I think you have an extra + sign when storing the IDs.
This is your loop:
foreach ( $postlist as $post ){
$posts[] += $post->ID;
}
Replace it with:
foreach ( $postlist as $post ){
$posts[] = $post->ID;
}
Try this, you do not need +
foreach ( $postlist as $post ) {
$posts[] = $post->ID;
}