Wordpresss - WP_query count - mysql

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.

Related

order a WP query by specific custom field, not working

I want to order some posts by the number of downloads, DESC, and it seems the articles are random displayed:
$posts = get_posts(array(
‘post_type’ => ‘post’,
‘posts_per_page’ => 12,
‘meta_key’ => ‘post_views_count’,
‘orderby’ => ‘meta_value’,
‘order’ => ‘DESC’
));
But strange, the query from Mysql works just fine:
`SELECT * FROM wp_postmeta WHERE meta_key = ‘post_views_count’ ORDER BY meta_value DESC`
Any help is appreciated, thanks
Meta_Value field stores values as a string. That's why it returns orderby data in alphabetical order.
f.e.
9,8,7,70,6,5,55,4,3,2,1
To get numerical orderby you should use meta_value_num.
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => 12,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
And also, in your question text, commas are wrong. It should be ', not ‘

WP_Query | meta_query argument meta value in array serialize form

In short i want get meta values from usermeta table but the meta value is in serialize array form,
these values are post ids actually, this is my working code for single meta value, i want multiple values from serialize array in meta_value
$user_id = get_current_user_id();
$key = 'classes';
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
$user_last;
$query_args = array(
'posts_per_page' => $output,
'post_status' => 'publish',
'post_type' => 'stm-courses',
'meta_query' => array(
array(
'key' => 'classes',
'value' => $user_last,
'compare' => 'LIKE'
)
)
);
print_r( $query_args ); echo "string";
the single meta value is working fine but not multiple values
the below is the output of the above query
Array ( [posts_per_page] => 3 [post_status] => publish [post_type] => stm-courses [meta_query] => Array ( [0] => Array ( [key] => classes [value] => Array ( [0] => 5033 [1] => 5034 ) [compare] => LIKE ) ) ) string
and in database the value for meta_key classes is stored something like this
a:2:{i:0;s:4:"5033";i:1;s:4:"5034";}
the values are dynamically changeable so i need something dynamic logic,
Thanks in advance, pls suggest me any good idea how i do this
$meta_query[] = array(
'key' => 'classes',
'value' => sprintf('"%s"', $user_last),
'compare' => 'LIKE',
);
I don't think Jagan's solution will work.
I've the same problem and if i look in the database, a meta_key in a serialize array are in the row meta_value (of an other meta_key )... Logic.
So if you try to get
meta_key => "classes",
i think the result will be null.
If someone have a solution to get the value of a meta_key in a serialize array with a wp_query ? I take it !

Optimizing a Wordpress Query

I'm working on a WordPress site with a large product database. I need to make a listing of all the products with a single taxonomy term exception. Using wp_query() creates a huge object and takes a long time. Also, for some reason, my arguments will not exclude products from the one taxonomy term I don't want listed. Here is my current query:
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'orderby' => 'post_title',
'numberposts' => -1,
'tax_query' => array(
'taxonomy' => 'product-main-cats',
'field' => 'term_id',
'terms' => array(23),
'operator' => 'NOT IN',
),
);
$prods = new WP_Query( $args );
I want to rewrite this query using the wpdb class so that I only fetch exactly what I need for my listing instead of the huge wp_query() object. The only columns I need from the wp_post table are, ID and post_title. I'm just not sure of the mySQL syntax, especially for excluding the posts related to the single taxonomy term. Can anyone help me rewrite this?
TIA!
You can write this query via $wpdb , I have attached a sql query version below of your WP_Query.
global $wpdb;
$results = $wpdb->get_results( "SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'products' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_title DESC", ARRAY_A );

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!

ISNULL not working in order in CAKEPHP

I am using LEFT join and as a result getting null values for is_read column in Messages table. I want to keep the nulls at bottom when ordering. I'm using this in paginator. Following is the my code for doing the same:
$this->Paginator->settings = array(
'fields' => array('User.*'),
'joins' => array(
array('table' => 'messages',
'alias' => 'Message',
'type' => 'LEFT',
'conditions' => array(
'User.id = Message.user_from_id'
)
),
),
'limit' => 20,
'group' => array('User.id'),
'order' => array('ISNULL(Message.is_read)' => 'asc','Message.is_read' => 'asc', 'Message.created' => 'asc'),
);
The query Cakephp generates for this is as follows:
SELECT `User`.*, (CONCAT(`User`.`first_name`, ' ', `User`.`last_name`)) AS `User__full_name` FROM `srs_development`.`users` AS `User` LEFT JOIN `srs_development`.`messages` AS `Message` ON (`User`.`id` = `Message`.`user_from_id`) WHERE 1 = 1 GROUP BY `User`.`id` ORDER BY `Message`.`is_read` asc, `Message`.`created` asc LIMIT 20
ISNULL function is getting omitted in the final query.
Also please suggest a way to accomplish this without using custom pagination() if possible.
Aggregate functions didn't work in the order clause when using Pagination component. I tried declaring a virtual field in Message model as:
public $virtualFields = array(
'sortme' => "ISNULL(Message.is_read)",
);
So finally, declaring it as virtual field in the Message model did the job.
Thank you everyone.