Crazy night! ;P
I'm devoliping a plugin with Wordpress and I'm lost looking for a function.
What I did is,
$args = array("meta_key" => "email", "meta_value" =>$email);
$posts = get_posts($args);
I did that in order to filter custom fields with the same email,
if (count($posts) < 0){ // do something }
The problem is that get_posts didn't work with custom posts,
$args = array('post_type' => 'reserva', "meta_key" => "wpcf-email", "meta_value" =>$email);
I like to find something like get_posts in order to count it.
I think I have a bad solution,: to loop it, but I want to array it.
Any idea?
Thanks,
Best regards,
Use meta_query in your arguments array.
Try this, if you want post related to meta_key and meta_value so please use this
http://codex.wordpress.org/Class_Reference/WP_Meta_Query
thanks a lot, I will check it now,
As I said tonight i did an ugly solution,
I made a loop with a counter in porder to find if a custom fields exist.
Here is the ugly code,
<?php // Find matches in csutom post types
$my_consulta = array(
'post_type' => 'reserva',
'post_status' => 'draft',
'post_key' => 'wpcf-email',
'meta_value' => $email,
);
$wp_query = new WP_Query($my_consulta);
$count = 0;
while ( have_posts() ) : the_post();
$count = $count + 1;
endwhile;
if ($count == 0){
//No matches
} else {
//There is one or more matches
}
?>
Is a good way?
Best regards,
Cristian
Related
I'm trying to make a custom WP_Query for a search results page.
With the following code, the page always displayed all posts regardless:
<?php
$args = array(
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
?>
So I've added the search Query to the $args, but this always returns no results - where is this going wrong?
<?php
$search_query = get_search_query();
echo $search_query;
$args = array(
'posts_per_page' => '-1',
's' => $search_query
);
$query = new WP_Query( $args );
?>
1) You can use the templatesearch.php and searchform.php as your starting points. Creating a Search Page Codex
2) As far as the custom query goes, you can use pre_get_posts hook to test if you're on a search page, then you get $_GET your values, edit your query accordingly. Action Reference - pre_get_posts
There are tons of tutorials online and questions on this exchange to help you out. Some are Simple and others are more Complex. You'll have to do some real research to accomplish this. Hope it helps!
I have solved this using pre_get_posts - I'm still intrigued as to why using the WP_Query method doesn't work, but here's what I'm now using:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', '-1');
}
}
}
add_action('pre_get_posts','search_filter');
is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks
If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!
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'
how do I put a custom var in the following line of the below
AND $wpdb->postmeta.meta_value = 'email'
I want to put in a var for 'email'
something like
AND $wpdb->postmeta.meta_value = $var
anyone know how to do this is Wordpress. I think i need to bind?
re: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
It depends a bit on context, but if you really are performing a custom query (as, say, part of a widget) you would provide it as an argument to WP_Query:
$query = new WP_Query( array( 'meta_value' => 'user#example.com' ) );
Oftentimes this would include a corresponding meta_key in that argument array.
If you want to modify the currently running query (say, on a category page, only show posts that match a certain criteria) you would perform that modification during the pre_get_posts action. Recommended reading: Andrew Nacin's You Don't Know Query.
I had done that using custom query plugin Go here
else custom your query like this
<?php
$args = array('post_type' => 'page','meta_query' => array(array('key' => 'email','value' => 'yes','compare' => '%')));
$var = new WP_Query($args);
// The Loop
while ( $var->have_posts() ) : $var->the_post();
$P_ID = get_the_ID();
endwhile;
// Reset Post Data
wp_reset_postdata();
Hope it helps
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();