I have the following code:
$sortable = false;
'post_type' => 'tour',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
$query = array(
'key' => 'rezdy_tour_type',
'value' => 'DAYTOUR'
)
)
);
I would love to add a specific script if my rezdy_tour_type == DAYTOUR, so I've tried:
<?php
global $post;
if(get_post_meta($tour->ID, 'rezdy_tour_type', true) == 'DAYTOUR') { ?>
mycustomscript
<?php
}
else { ?>
<?php
}
?>
however I am not able to create the script. Some inside help would be truly appreciated, thank you
Try replacing your:
$tour->ID with
$post->ID.
From what i see (in second code) that that $tour variable is undefined. Maybe that helps?
I am assuming that you are finding the post list which have the DAYTOUR in their meta values. If my assumption is correct then you can use below code it should work well.
$argument= array( 'post_type' => 'tour',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'rezdy_tour_type',
'value' => 'DAYTOUR',
'compare' => '=',
)
)
);
Related
I really don't know what's the issue. I think my code is OK but the output is wrong. I don't know anything about WordPress, please help me.
elseif ($_GET['search']) {
$args = array(
'post_type' => 'head_to_toe_videos',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $_GET['search'],
'compare' => 'LIKE',
)
),
'posts_per_page' => 12,
);
}
Your query is correct but need to execute you query like this:
$the_query = new WP_Query( $args );
$result = $the_query->get_results();
echo "<pre>"; print_r($result); exit;
Try the below code your problem will solve.
elseif ($_GET['search'] != '') {
$args = array(
'post_type' => 'head_to_toe_videos',
'post_status' => 'publish',
'meta_key' => 'post_title',
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $_GET['search'],
'compare' => 'LIKE',
)
),
'posts_per_page' => 12,
);
$result = new WP_Query( $args );
}
I am using two loop query:
<?php
// show all coupons marked Top Coupon
query_posts(array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'meta_key' => 'clpr_topcoupon',
'meta_value'=> 1,
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => 1
));
?>
<?php get_template_part( 'loop3', 'coupon' ); ?>
<?php
query_posts( array(
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'clpr_excoupon',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'clpr_excoupon',
'compare' => '!=',
'value' => '1'
),
),
) );
?>
<?php get_template_part( 'loop1', 'coupon' ); ?>
Now I don't want to show the first post from the first loop in the second loop. I tried get_the_ID(); however if this one is not having the 'meta_key' => 'clpr_topcoupon' one post is missing. How do I get the get_the_ID(); from first post but only if it has the 'meta_key' => 'clpr_topcoupon'?
Wordpress docs suggest that you should avoid using query_posts whenever possible stating:
Note: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible.
Instead we can use WP_Query . We'll use the first loop to store the post id and check it during the second loop. Maybe something like this:
<?php
//set parameters for First query
$args = array('post_type' => APP_POST_TYPE,
'post_status' => 'publish',
'meta_key' => 'clpr_topcoupon',
'meta_value'=> 1,
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => 1 );
$first_query = new WP_Query($args); // create query
$post_id = 0;
//initialize loop for custom query like this
if ($first_query->have_posts() ) {
while ($first_query->have_posts() ) {
$first_query->the_post();
$post_id = $post->ID; //store post ID outside of loop
get_template_part( 'loop3', 'coupon' );
}
}
wp_reset_postdata();
//setup second query
$args = array( //excludes post from query by ID See Bill erikson for complete list of WP_Query() arguments
'post__not_in' => array($post_id),
'post_type' => APP_POST_TYPE,
'post_status' => 'publish',
APP_TAX_STORE => $term->slug,
'ignore_sticky_posts' => 1,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'clpr_excoupon',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'clpr_excoupon',
'compare' => '!=',
'value' => '1'
)
)
);
$second_query = new WP_Query($args);
if ($second_query->have_posts() ) {
while ($second_query->have_posts() {
$second_query->the_post();
get_template_part( 'loop1', 'coupon' );
}
}
wp_reset_postdata();
Hopefully, this code is able to assist you. As you can see, WP_Query accepts an argument 'post__not_in' that takes an array of page id's and excludes them from the query. We retrieved the id from the first query and referenced it in the argument of the second query. I also included wp_reset_postdata which is worth taking a look at if you're running multiple queries.
Good luck with your project!
I have a loop that I am building a filter for, using a custom taxonomy.
It works, but only when there is a value entered. When the page loads it shows no entries, but when I filter by a value it shows the items with that tag. I thought that 'LIKE' would allow it show all entries while the value is empty.
I have tried this by removing the $ingredients and replacing it with an empty ''.
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'ingredients',
'value' => $ingredients,
'compare' => 'LIKE'
)
)
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();?>
<?php the_title(); ?><br />
<?php endwhile; wp_reset_query(); ?>
Check by passing false to the argument,
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'ingredients',
'value' => $ingredients,
'compare' => 'LIKE'
),
array(
'key' => 'ingredients',
'value' => false,
'type' => 'BOOLEAN'
),
)
);
Also add compare => like to the false condition and cross check.
I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.
I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;