Getting 'meta_value' when searching for matching 'post_id' and 'meta_key', 'meta_value' combo

AndrewTelkamp

postmeta table example

The end result is matching an array of 'post_id', sorted by the number of attendees. I know it is a WP backend but I have to do it in SQL, no 'get_posts' or anything WordPress related. What I'm wanting to do is confusing so I'll try to be clear.

What I Have To Begin:

  • $check_post_ids - the original array of post_ids I need to check
  • $start_date - the 'meta_value' each event's '_StartDate' needs to match.

What I Need To Do:

I need to check these three post_ids to see if they have a matching start date. If they do, I need to get an array of post_ids sorted from the highest to lowest number of attendees.


Currently I was planning on doing this with multiple SELECTs and foreach() statements, but I feel like there has to be a simpler way to do this ( i.e. One SELECT & foreach() ). Here's what I'm doing at the moment. I haven't finished it yet because I feel like there has to be a simpler way to do this. Newer to SQL and any help is tremendously appreciated!

    $check_post_ids = array('484', '627', '982', '2435');       
    $start_date = '1963-10-20 19:30:00';            

    // iterate through array of post_ids and check if they have the same _StartDate
    foreach($check_post_ids as $id){
        $start_date_check = "SELECT * FROM `wp_postmeta` WHERE `post_id` =" . $id . " AND `meta_key` LIKE '_StartDate' AND `meta_value` = '" . $start_date . "'";
        $start_date_check_result = mysqli_query($conn, $start_date_check);

        // assign all post_ids with a matching _StartTime to a new array
        if (mysqli_num_rows($start_date_check_result) > 0) {
            while($row = mysqli_fetch_assoc($start_date_check_result)) {
                $matching_post_ids[] = $row['post_id'];

                // iterate through matching_post_ids array, get the _NumAttendees for each, and assign their id and _NumAttendees to an assoc array to be sorted
                foreach($matching_post_ids as $id){
                    $attendees_check = "SELECT meta_value FROM wp_postmeta WHERE post_id = " . $id . " AND meta_key = '_ecp_custom_2'";
                    $attendees_check_result = mysqli_query($conn, $attendees_check);
                    if($upvotes_check > 0){
                        while($row = mysqli_fetch_assoc($attendees_check_result)){
                            $sort_by_attendees['id'] = $id;
                            $sort_by_attendees['attendees'] = $row['meta_value'];
                            $sort_by_attendees_array[] = $sort_by_attendees;
                        }
                    }
                } 
Arrabidas92

For the first part of the query, I think you can simplify your code by using SQL IN keyword. Basically it substitutes the role of your first array with all your post IDs. Then, you can write a SQL query like this :

SELECT meta_value 
FROM wp_postmeta 
WHERE meta_key = '_ecp_custom_2' 
AND post_id IN (SELECT post_id
FROM wp_postmeta 
WHERE post_id IN ('484', '627', '982', '2435')
AND meta_key LIKE '_StartDate' 
AND meta_value = '" . $start_date . "'")
ORDER BY meta_value DESC

There are 2 queries. The first one in the parenthesis, subselect all the post ids on your table wp_postmeta where post_ids have ids in your list and if they match with your starting date. Then, the main query selects all meta_values (I suppose attendees) based on your first subquery (all post ids with your starting date).

Hope it will help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

wordpress get post_id where meta_value =

Retrieve Post by Custom Query String identified by meta_key and meta_value in Wordpress

ACF meta_value assigning page id

Filter by custom field 'meta_key', 'meta_value' for archive

Construct MySQL query ( meta_key/meta_value table)

WP_Query when meta_value saved as serialized array

Order custom post types in WordPress by the meta_value of another plugin

Get all products with specific meta_key whose meta_value is equal in WooCommerce

WP SQL add meta_key with an empty meta_value to all users

How to count how many times a meta_value appears in a column by certain meta_key?

Update product price if specific meta_key contains a specific meta_value

Group similar meta_key values and create result set with meta_value data

Sql query to exclude meta_value where meta_key = x on SUM for WooCommerce

CONCAT(meta_key,meta_value) into different columns from wp_postmeta table

How to copy meta_value of wp_usermeta to the same table however different meta_key?

SQL query with meta_key,meta_value and have 2 conditions

How to use WordPress metaquery when meta_value is saved as serialized key/value

Get Meta_Value from WP database

Inner join on a meta_value field

Update meta_value for specific word

WP REST API orderby meta_value

Order by meta_value in SQL request

wordpress - to find if a meta_value exists

Looking for query to get meta_value

Update table wp_postmeta column meta_value where meta_key column equals certain value

How to display meta_value only once if the value is the same

Specific SQL query (multiple meta_value / same row)

orderby price meta_value which contains comma - wordpress

How is the Advanced Custom Fields meta_value determined?

TOP Ranking

HotTag

Archive