wordpress query 2 sets of meta keys - mysql

I am trying to do a custom sql query to get 2 lots of post_meta keys and there values. Here is my code:
<?php
$queried_post = get_post($post_id);
$title = $term->name ;
$userid = $current_user->user_email;
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key='user_programme_name' AND meta_value='".$title."' AND meta_key='user_email' AND meta_value='".$userid."' ");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0 ) {
?>
<p style="width:430px;">You have already requested a place on this course</p>
<?php } ?>
The first part of the query works when i jsut query the first set of keys and values:
meta_key='user_programme_name' AND meta_value='".$title."'
But as soon as i add the second lot:
AND meta_key='user_email' AND meta_value='".$userid."'
It doesn't work. Is there something I'm doing wrong?
Any help would be greatly appreciated.
Cheers, Dan

Use something like this :
$args = array( 'post_type' => 'myposttype',
'posts_per_page' => …,
'offset' => …,
'meta_query'=>array(
'relation' => 'AND',
array(
'key'=>'_first_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
),
array(
'key'=>'_second_key',
'value'=> youvalue,
'type' => 'DATE', // etc.
'compare' => '='
)
)
);
$yourloop = new WP_Query( $args );

Fix your SQL syntax for selecting multiple values on the same column:
<?php
$result = mysql_query("SELECT * FROM wp_postmeta WHERE meta_key IN ('user_programme_name', 'user_email') AND meta_value IN ('.$title.', '.$userid.') ");
?>

Related

Wordpresss - WP_query count

I need to count how many times my date recorded in the meta key:metakey_AMC_data, in format (d-m-Y) it is contained in the database by comparing it with the current date
$mostra_data_corrente = date('d-m-Y');
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')");
$conta_risultati = count($query);
and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect
SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')
AND(slug = 'aperitivi') "
how can i do this?
You can get that count as well. You need to modify query (code) like follow:
$qry = array(
'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
'meta_query' => array(
array(
'meta_key' => 'metakey_AMC_data',
'meta_value' => $mostra_data_corrente,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
'field' => 'slug',
'terms' => 'aperitivi',
)
)
)
$the_query = WP_Query($qry);
echo $the_query->post_count;
You have to make some necessary changes in above code to suite your requirements. I've added comment where you have to do changes.

how to filter wordpress select query with two taxonomy conditions?

I want to select records from wordpress database based on two taxonomy conditions.
I have the following query.But it lists all the records.I want to filter the following query with taxonomy recipe-category and term 90 .How can i do this?
select DISTINCT p.ID, p.post_author,p.post_date as dte,wt.term_id from
wp_posts p left JOIN wp_postmeta m1 ON p.ID = m1.post_id left JOIN
wp_term_relationships wtr ON (p.ID = wtr.object_id) left JOIN
wp_term_taxonomy wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
left JOIN wp_terms wt ON (wt.term_id = wtt.term_id) where
p.post_type='recipe' and p.post_status='publish' AND (wtt.taxonomy =
'recipe-cuisine' and wtt.term_id IN (17) ) order by dte DESC
Why not use WP_QUERY to get posts related to taxonomy (recipe-category) and the term of recipe-category id 90
$taxonomy = 'recipe-category';
$taxonomy_terms = 90;
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
),
),
);
$posts_related_to_taxonomy = new WP_Query($args);
if( $posts_related_to_taxonomy->have_posts() ) :
echo '<p>';
while($posts_related_to_taxonomy->have_posts()) : $posts_related_to_taxonomy->the_post();
?>
<a href="<?php the_permalink(); ?>">
<span><?php the_title(); ?></span> : <?php echo get_the_excerpt(); ?>
</a>
<br>
<?php endwhile;
echo '</p>';
else :
echo wpautop('No Records found');
endif;
If you want to search between two taxonomy then replace the tax_query argument with the following
'terms' => array( $term) this argument can be use with array of term ids or string , replace taxonomy1 and taxonomy2 with your other taxonomies slug
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'taxonomy1',
'field' => 'id',
'terms' => array( $term)
),
array(
'taxonomy' => 'taxonomy2',
'field' => 'id',
'terms' => array( $term2),
)),
well, the easier method is to use WP's get_posts template function. Looks like parameter category is what you need. AFAIK it allows to use any taxonomy term ID, not only categories.
If you still want to use a direct sql query - it will be easier to get an answer if You'll format the query in question in more readdable way.

ACF Field query by Repeater Field not empty

Been reading up on info for querying ACF Fields in wordpress here, having trouble with this bit though:
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'patents',
'value' => array(''),
'compare' => 'NOT IN'
)
)
);
$the_query = new WP_Query($args);
Basically, just trying to query all posts that have an ACF Repeater field, called patents that has at least 1 patent inside of it. How to do this?
I just had to deal with this myself, and what ended up working for me was querying for all posts where the value of the repeater field was greater than 0.
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'patents',
'value' => 0
'compare' => '>'
)
)
);
Mark's answer to search for 'patents' = 1 only returned posts where the repeater field had one value.
http://www.advancedcustomfields.com/resources/the_repeater_field/
Try to access the repeater field first, then call the WP_Query. If you were to do something like:
if( get_field('repeater_field_name', $post_id) )
Then call the sub_field and/or in this case the WP_Query.
There is also this syntax, you can gather all the posts you need then scroll through posts:
if( have_rows('repeater_field') ):
while( have_rows('repeater_field') ) : the_row();
$sub = get_sub_field('sub_field');
Maybe you can call the wp_query on this level?
just check if the value is true
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'patents',
'value' => true,
'compare' => '='
)
)
);
$the_query = new WP_Query($args);
Try this ..
This is not the standard solution for achieving the target but it will
work
Get all the post IDs whose repeater field(s) are there
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( have_rows('patents') ){
$allowed_ids[] = get_the_ID();
}
}
}
/* Restore original Post Data */
wp_reset_postdata();?>
Then run another Loop to get only those posts by their IDS
$args['post__in'] = $allowed_ids;
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$patents = get_field('patents');//array
}}

Wordpress WP_Query Multiple Queries

I'm trying to come up with a way of querying a CPT by a specific author but also if a meta_key has a specific value.
For example, my CPT has an author ID attached to it as well as a 'recipient' custom field. What i need is all posts of this type by this author as well as all posts of this type where 'recipient' = this author.
(Hope I've explained that well enough!)
I'm usually pretty OK with SQL but just having a random brain fart moment and can't figure this one out. Like I said, any help is much appreciated!
== Solved ==
Thanks to #3pepe3 for suggesting the meta_query parameter of WP_Query. Modified my CPT logic slightly and now have a beautifully working system.
The solution was to add a new meta field for each post containing the original sender's user id, then run two meta_queries to retrieve posts where the current user_id matches either the sender or recipient fields, likes so:
$args = array(
'post_type' => 'activity',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'sender',
'value' => $user_id,
'type' => 'BINARY',
'compare' => '='
),
array(
'key' => 'recipient',
'value' => $user_id,
'type' => 'BINARY',
'compare' => '='
)
)
);
$query = new WP_Query( $args );
Helo JRM47R1X,
Instead of using SQL queries you should use the WP_Query Class
<?php
$args = array(
'post_type' => 'activity',
'author' => $user_id,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'recipient',
'value' => $user_id,
'type' => 'BINARY',
'compare' => '='
),
),
);
$query = new WP_Query($args);
?>
The advantage of using the WP_Query is that you can mix meta queries, taxonomies and dates with logical operands like OR, AND BETWEEN....
Ok I eventually figured it out. Turns out all I needed was a Union on the two queries like so:
SELECT * FROM (
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE $wpdb->posts.post_author = $user_id
AND $wpdb->posts.post_type = 'activity'
AND $wpdb->posts.post_status = 'publish'
UNION
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key = 'recipient'
AND $wpdb->postmeta.meta_value = $user_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_type = 'activity'
AND $wpdb->posts.post_status = 'publish'
) as x
ORDER BY x.post_date DESC
Hope this helps anyone else having this bizarrely obscure problem!

Wordpress mysql query not showing results?

The query should return thumbnail, title, name, price etc (all the fields)
<?php
query_posts('meta_key=cp_job&meta_value=Sell');
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
the_content();
//
} // end while
} // end if
?>
It should return results where meta_key=cp_job and meta_value=Sell.. I've tried all sorts of queries and this has taken up several hours, as i've yet to find a solution.
I'm working with a theme and the only time i've gotten a result is with this query
$metakey = 'cp_job';
$job = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
foreach ($job as $value) {
echo $value";
}
This query doesn't do what i want to accomplish.
I simply want "SELECT * FROM table WHERE cp_job='Sell'"; but WordPress makes everything so complicated. I don't even know the table which hold my results!...
Any help please..
You may try this:
$args = array(
'post_type' => 'post', // Or custom post type if it's a CPT
'meta_key' => 'cp_job',
'meta_query' => array(
array(
'key' => 'cp_job',
'value' => 'Sell'
)
)
);
Then run the query and loop the $query same way:
$query = new WP_Query( $args );