I have 2 custom meta one for the start date of my event and one for the end date of my event and I need to order my events according to the start date and to remove them when the end date is over.
Meta:
Start date: date_de_levenvement
End date: date_de_fin
Here is my loop:
<?php query_posts( 'post_type=agenda&meta_key=date_de_fin&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$date = get_field('date_de_fin');
$newDate = date("d/m/Y", strtotime($date)); ?>
Just need to change the query but I don't know how to use 2 parameters with the meta value. &orderby=meta_value needs to be something like &orderby="date_de_levenement"
I would actually use the more robust WP_Query object to accomplish this task. More specifically, you can see the section called Custom Field Parameters and see the example titled Multiple Custom Field Handling. You can create your arguments array like so:
$args = array(
'post_type' => 'agenda',
'meta_key' => 'date_de_levenvement',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $todaysDate,
'compare' => '>='
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
// The Loop
if ( $your_custom_query->have_posts() ) {
while ( $your_custom_query->have_posts() ) {
$your_custom_query->the_post();
// your code goes here
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Explanation
The meta_query element is an array of arrays which means you can even filter against multiple meta (custom field) properties if you wish. Notice that I included the meta_key element. This is what will allow you to sort by your start date. Whereas the key element of the meta_query element is what will filter your end date posts. (In this situation, post types named agenda will only be included if their date_de_fin is greater than or equal to "today".
[edit]
Note that the "today" date should be formatted in SQL format like so:
<?php $todaysDate = date('Y-m-d'); ?>
Hope this helps a bit. Have fun!
Related
I am trying to create a custom loop which gets the posts that author__in 1 has commented the specific keyword 'test' on and use that to build the recent posts list.
<?php $query_args = array('search' => 'test', 'author__in' => '1', 'post_status' => 'publish', ); $the_query = new WP_Comment_Query( $query_args );?> <?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
This query doesn't work, it gives me nothing, i guess ->have_posts does not exist for wp_comment_query?
How can i build a query using WP_query that lists the most recent posts with comment 'test' from author with id 1? Is it even possible?
Couple things here.
First, the "search" parameter is actually only "s", not the full word. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Second, the author__in param takes an array, like this: $query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) ); rather than a single integer or quoted digit. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
I think you could get what you want by just changing your code slightly. One thing to note, I'm not sure if the search parameter does a keyword search in comments though, it might only search post content. Try this:
$query_args = array('s' => 'test', 'author__in' => array(1), 'post_status' => 'publish', );
Let me know if this works for you.
I have defined multiple custom post types in my wordpress installation. I want to get latest posts from all the custom post types. all resources and tutorials I look into describes only getting latest post from one custom post type, not multiple.
is there any way to do this? for example assigning multiple values to post_type attribute in WP_Query objects or wp_get_recent_posts() function? if answer is yes exactly how to do this.
any help would be appreciated.
It will fetch posts from multiple custom post type.
query_posts( array(
'post_type' => array( 'custom_post1', 'custom_post2', 'custom_post3',
'custom_post4' ),
'cat' => 3,
'showposts' => 5 )
);
First thing I want to clear here I do not know about WordPress but I can help you with the SQL query
I assume you have date time column in your table .
select * from table name
where column name in (here write all your different types followed by comma )
order by your date time column name desc;
E.g.
select * from posts where type in(1,2,3,4) order by created_on desc;
Let's fetch your all custom post types.
$args = array('public' => true, '_builtin' => false);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
The $post_types is now an array containing all the custom post type names. Your all posts query should be like this
$allposts = array( 'posts_per_page' => -1,
'post_type'=> $post_types,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query( $allposts );
if ( $query->have_posts() ) :
while ($query-> have_posts()) : $query -> the_post()
the_permalink();
the_title();
the_content();
endwhile;
endif;
I'm currently working on a small blog which has a sidebar that displays all "special projects".
"Special projects" is just a category, the sidebar will only display 4 posts at a time which are filtered by post date, but I also have a custom meta box to allow the user to feature posts.
These featured post should show up on the top of the special projects.
Right now my query is like this:
new WP_Query("showposts=" . $instance['num'] . "&cat=" . $instance["cat"] . "&order=ASC&order_by=date")
And I can get the featured meta data like this:
$featured = get_post_meta($single_cat_post->ID, 'soy_featured_post', true);
But how could I integrate this inside the WP_Query?
First of all, 'showposts' has been replaced by 'posts_per_page' when using WP_Query. I've corrected that in your code. Also, within a loop you should just be able to use $post->ID instead of $single_cat_post->ID.
I would use two loops. Set your paramaters, then in the first loop include a condition to check for the meta value, reset the query, then do another loop and include a condition that checks for the meta value and outputs nothing if it exists.
In the first query, I added a check to see how many posts are returned by the first loop. Then using that value (subtracted by 4) I calculated a variable to use for posts_per_page in the second loop. Then I added a conditional to only run the loop if the result is greater then 0.
This is untested, but it should work or at least put you on the right path!
<?php
$args = array(
'posts_per_page' => 4,
'meta_key' => 'soy_featured_post',
'cat' => $instance["cat"],
'orderby' => 'date',
'order' => 'ASC'
);
$special_post_query = new WP_Query( $args );
$special_posts_found = $special_post_query->found_posts;
if ($special_post_query->have_posts()) :
while( $special_post_query->have_posts() ) : $special_post_query->the_post();
// POST WITH META VALUE OUTPUT
the_title();
endwhile;
endif;
wp_reset_query();
$second_loop_posts_per_page = 4 - $special_posts_found;
if ($second_loop_posts_per_page > 0) {
$args = array(
'posts_per_page' => $second_loop_posts_per_page,
'cat' => $instance["cat"],
'orderby' => 'date',
'order' => 'ASC'
);
if ($special_post_query->have_posts() ) :
while( $special_post_query->have_posts() ) : $special_post_query->the_post();
// Condition to test for NO meta value
if (get_post_meta($post->ID, 'soy_featured_post', true) == null) {
// CODE
the_title();
} else {
// Don't print anything because the meta value exists
}
endwhile;
endif;
wp_reset_query();
} ?>
I want to use
$ps = "9126,8955,8554,8620,8825,8912,8937,8813,9054,9022";
$recent = new WP_Query( array('post__in' => explode(',', $ps)) );
to retrieve posts by ID, I do and the result ordered by publish date but I want to retrieve in same of IDs in this example in this order:
9126,8955,8554,8620,8825,8912,8937,8813,9054,9022
Personally, if the order of posts as well as your ID numbers are going to be that strict, I would probably avoid using WP_Query:
<?php
$ids = array(9126,8955,8554,8620,8825,8912,8937,8813,9054,9022);
foreach($ids as $id)
{
$post = get_post($id);
setup_postdata($post);
?>
<!-- YOUR HTML HERE -->
<?php
}
?>
Otherwise, since Posts do not have any kind of Menu Order option like Pages do, you will probably have to set a Custom Field and give it a value (with lower numbers taking priority over higher numbers). Then you can do something like this:
<?php
$ids = array(9126,8955,8554,8620,8825,8912,8937,8813,9054,9022);
$recent = new WP_Query(array('post__in' => $ids, 'orderby' => 'meta_value_num', 'meta_key' => 'postOrder', 'order' => 'ASC'));
?>
This second option is untested and I can't guarantee it will work right off the bat, but it should get you started. Good luck.
in the future in wordpress 3.5 you can use: 'orderby'=>'post__in'
There are tutorials that explain how to limit a search to a specific category.
My question is, is there a way to configure wordpress' search to, within a custom post type, search for a custom field value.
So for example, if I search for "hello", the results would come up with posts that have a certain custom field equal to "hello". The certain post would also be a certain custom post type.
Any help is appreciated.
To filter search by custom post type use:
<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
before the loop.. then within the loop add a condition similar to this
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
//do something
} ?>
I think here is what you are looking for:
key is the custom field.
value is the value you are looking for
and compare is the operator you want to use.
you can also use LIKE if you want.
// WP_Query arguments
$args = array (
'post_type' => 'vendors',
'post_status' => 'published',
'meta_query' => array(
array(
'key' => 'state',
'value' => 'Misissipi',
'compare' => '=',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();