I have my page displaying only future dates which is just what I want, I have been digging around trying to find a way to query only: posts that are dated prior to todays date.
This is currently my query:
<?php
$args=array(
'post_type' => 'artists',
'orderby' => 'ecpt_featured_month',
'order' => 'ASC',
'post_status' => 'future'
);
Any ideas? If it helps, each post is an artists and they have a featured month, post = month.
Thanks,
Ryan
Use this one considering "ecpt_featured_month" is a custom field:
$args=array(
'post_type' => 'artists',
'orderby' => 'meta_value',
'meta_key' => 'ecpt_featured_month'
'order' => 'ASC',
'post_status' => 'future'
);
$loop = new WP_Query( $args);
while ( $loop->have_posts() ) : $loop->the_post();
// write the general code to display the post's title and date
endwhile;
Related
I use ACF plugin on my wordpress site and I want to code custom research.
I have many products and I want to display products which contain the searched word.
I do my query like this :
$args = array(
'post_type' => 'product',
'meta_key' => 'brand',
'meta_value' => $word,
'compare' => 'LIKE');
$the_query = new WP_Query($args);
But this display only products with the brand which exactly matches with $word.
for exemple if I search "yan" I want to display products with the brand "YANMAR", "POLYAN", "TRYANPO", etc.
How to do this please ?
Thank you and have a good day !
Try below code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'brand',
'value' => $word,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args);
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 need to get_pages or get an array of pages in WP based on it 's category
I know WP doesn't come with categories on pages but I'm using this in the functions.php to get categories on the pages.
add_action('admin_init', 'reg_tax');
function reg_tax() {
register_taxonomy_for_object_type('category', 'page');
add_post_type_support('page', 'category');
}
Now I need to use these categories in get_pages or WP_Query to get in the pages with a category.
<div class="productNav">
<ul>
<?php
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'category_name' => 'pillar-product'
);
//$product_pages = get_pages($product_page_args);
$product_pages = new WP_Query($product_page_args);
foreach ($product_pages as $product_page){
?>
<li><?php echo $product_page->post_title; ?></li>
<?php
}
?>
</ul>
</div>
Respectfully this answer does NOT work anymore in 2021. This is an obsolete answer and includes all of the pages. Please use the other answer.
Try this: (won't work on wordpress 5.8)
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'taxonomy' => 'category',
'field' => 'slug',
'term' => 'pillar-product'
);
The accepted answer for this question won't work anymore and includes all of the pages because 'taxonomy', 'field' and 'term' must be inside an array which resides inside 'tax_query' array, for this query to work.
For more comprehensive answer on how to query wordpress pages based on specific category see the following link:
How to query pages based on specific categories
Here's the correct answer for this question, fully tested on wordpress 5.8.
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('pillar-product'),
)
)
);
On a single Post page I have a side bar displaying up to three other, related posts. How can I exclude both Sticky Posts and the Current post?
I know how to exclude the Current post and how to exclude Sticky Posts by using post_not_in in a WP_Query, see code example below. But I guess you can not use post__not_in twice in the same query. Any suggestions?
$current_post_ID = get_the_ID();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' )
'post__not_in' => array($current_post_ID)
);
The post__not_in is an array() and uses post ids (numbers only).
There is no need to use it twice, use it like that:
//First build the array of excluded posts
$excluded_posts = array_push(get_option( 'sticky_posts' ) , $current_post_ID);
//Then use it in the option
'post__not_in' => array( $excluded_posts )
Note the array_push() PHP function, this will add the current post to the end of the sticky posts array, which is then passed to 'post__not_in' option.
<?php
$sticky =array(get_option('sticky_posts'));
// (add post id on sticky_posts option like ex. 485,458,256)
$current_post_ID = get_the_ID();
array_push($sticky,$current_post_ID);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => array($sticky)
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<!-- add your code which to display hear -->
<?php
endwhile;
wp_reset_query();
?>
im a bit stuck at the moment with the order in my wp_query.
I want to sort a query firstly by meta_value_num (this works perfect) AND as fallback by the date. But in my query the date seems dominant in comparision with the meta_value_num.
So it sorts all my posts by date and then apllys the meta_num_value order and not vice versa.
Do you have any clue how to do this?
$args = array(
'post_type' => 'anbieter',
'showposts' => -1,
'order' => 'DESC',
'orderby' => 'meta_value_num date',
'meta_key' => 'rating'
);
I found so many threads to order by two custom fields but not to sort by by "normal post field" AND custom field.
regards,
I found a solution a while ago which was this. Add this to your functions.php:
function wdw_query_orderby_postmeta_date( $orderby ){
$new_orderby = str_replace( "wp_postmeta.meta_value", "STR_TO_DATE(wp_postmeta.meta_value, '%d-%m-%Y')", $orderby );
return $new_orderby;
}
Then do this with your query:
add_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);
$args = array(
'post_type' => 'anbieter',
'showposts' => -1,
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'rating'
);
remove_filter( 'posts_orderby', 'wdw_query_orderby_postmeta_date', 10, 1);