* This question may be quite long so hope to bear with me *
Was trying to group address under a particular city dynamically.
Layout sample:
So based on the sample layout above, Cupertino belongs to CA and so is displayed below it.
The city is selected inside an ACF group.
I tried to query the cities by the following code:
<?php
$counter = 0;
$cityArray = array();
?>
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'orderby'=>'post_id', 'order'=>'ASC' ) ); ?>
<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$city = get_field('city');
if(!in_array($city, $cityArray))
{
$cityArray[$counter] = get_field('city');
$counter++;
}
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php
foreach ($cityArray as $i)
{
$loopCity = $i;
?>
<?php echo "city before loop is $i"; ?>
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> '$i', 'order' => 'ASC' ) ); ?>
<?php echo "city after loop is $i"; ?>
The value of the $i variable do display as the sample below shows:
However, the WP_Query seems to not recognize the variable $i.
If I used the following:
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> 'Los Angeles', 'order' => 'ASC' ) ); ?>
instead of:
<?php $loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> '$i', 'order' => 'ASC' ) ); ?>
The query outputs all address under Los Angeles.
What could I be doing wrong?
Seems that you're passing on a variable as string.
Try:
$loop = new WP_Query( array( 'posts_per_page' => -1, 'post_type' => 'branches_locations', 'meta_key' => 'city', 'meta_value'=> $i, 'order' => 'ASC' ) );
Related
I'm trying to show products from a certain category on a page like his:
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'prcategory1', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
This one works. However, I'd like 'prcategory1' to be taken from a custom field of the page. Something like this (incorrect code incoming):
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'get_post_meta(get_the_ID(), 'custom_cat_name', TRUE); ?>', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
There is some errors in your code, try this for your array :
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => get_post_meta(
get_the_ID(),
'custom_cat_name',
TRUE
),
'orderby' => 'price'
)
Question for all of ya. I have the current query running on my search page template, and it seems to be working fine if my search query seems to be included in the title of the post, but when I include a meta query to try and also look in another spot for the search term, it doesn't gather any results, only the same results it had before.
Second question, for some reason it still is only displaying 6 (the number of posts set in WP Admin) posts, and not listening to the query.
<?php // WP_User_Query arguments
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => '*'.$search_term.'*',
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$wp_course_query = new WP_Query($args);
// Get the results
$courses = $wp_course_query; ?>
<?php // Check for results
if (!empty($courses->get_posts())) { ?>
<ul class="course-list">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li> <?php the_title(); ?> </li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
<?php } else { ?>
<p>No courses match that query</p>
<?php } ?>
Things I've tried:
Hard coding the value, nothing there.
Removing * from 's'
It seems that this is just impossible in WordPress, so I had to do this another way.
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => $search_term
);
$args2 = array (
'post_type' => 'courses',
'posts_per_page' => -1,
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$courses1 = get_posts($args);
$courses2 = get_posts($args2);
$merged = array_merge($courses1, $courses2);
$post_ids = array();
foreach ($merged as $item) {
$post_ids[] = $item->ID;
}
$unique = array_unique($post_ids);
$posts = get_posts(array(
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $unique,
'posts_per_page' => -1
)); ?>
I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);
I am trying to show only those events, which are in current date or in future. To show events I have such code:
<?php
$wp_query = new WP_Query();
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0;
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
while ( ($wp_query->have_posts()) ): $wp_query->the_post();
get_template_part( 'content', 'event' );
endwhile;
?>
I tried to add "IF statement" to compare event start date with current date, but it doesn't seem to work. I have tried a lots of versions, but this one I think should work:
$wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
$OstartDate = get_post_meta($post->ID, '_event_start', TRUE);
$today = date('d.m.Y');
while ( ($wp_query->have_posts()) && ($OstarDate < $today) ):
$wp_query->the_post();
From the second one you should try,
$OstartDate=get_post_meta($post->ID,'_event_start',TRUE);//check it will return a number
if(time() > $OstartDate)
{
while ($wp_query->have_posts()):
$wp_query->the_post();
.....
}
a7-simple-events may help you.
You can compare the posts with postmeta using meta_query
$today = date('d.m.Y');
$args=array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(
array(
'key' => '_event_start',
'value' => $today,
'type' => 'date',
'compare' => '<'
)
)) ;
$wp_query = new WP_Query( $args );
while ( ($wp_query->have_posts()) ):
$wp_query->the_post();
endwhile;
How can I skip the first post in WordPress?
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array('showposts' => 6,'post_type' =>array('stiri')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
Use the offset parameter:
<?php
$recentPosts = new WP_Query( 'offset=1' ) );
$recentPosts->query(array('showposts' => 6,'post_type' =>array('stiri')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
Use the offset
$recentPosts = new WP_Query (
array(
'post_type' => 'stiri',
'post_status' => 'publish',
'posts_per_page' => 6, // all = -1
'orderby' => 'date',
'order' => 'DESC',
'offset' => 1
)
);