MySQL Help in Wordpress (Basic, I think but not sure!)
How can I find distinct Property values within a table based on their Area value and their post_id must match? Let's say I want Property values for Arabian Ranches based on the below table. The result I'm looking for is Avenida 1, Avenida 2.
post_id 104908, meta_key Property, meta_value Avenida 1
post_id 104908, meta_key Area, Arabian Ranches
post_id 104909, meta_key Property, meta_value Avenida 2
post_id 104909, meta_key Area, Arabian Ranches
post_id 104910, meta_key Property, meta_value Al Arta 1
post_id 104910, meta_key Area, Greens
post_id 104911, meta_key Property, meta_value Avenida 2
post_id 104911, meta_key Area, Arabian Ranches
We know which Property and which Area belong together based on their rows having matching post_ID's.
Solutions
Asuming you don't mind having therm displayesdin a nice comma
separated list
:
SELECT meta_key, GROUP_CONCAT( DISTINCT meta_value SEPARATOR ', ' ) AS meta_values FROM wp_postmeta GROUP BY meta_key HAVING meta_key LIKE '%Area%';
This will produce something like this:
meta_key | meta_values
------------------------------------------------
Area 1 | Value 1, Value 2
Area 51 | Aliens, More Aliens, Even more aliens
If you don't want it groupped, you can always return separate rows:
SELECT DISTINCTROW meta_key, meta_value FROM wp_postmeta WHERE meta_key LIKE '%a%' ORDER BY meta_key, meta_value;
Which will produce
meta_key | meta_value
----------------------
Area 1 | Value 1
Area 2 | Value 2
Area 51 | Aliens
Area 51 | More aliens
Hope this helps.
Similar questions
add_rewrite_rule() not stored (I think)
I'm developing a plugin that will set up a new custom post type (tournament) and which creates posts for this CPT using an API that gets called using WP Cron. The CPT has a custom field called season that I want to use in the permalink. I have set up the permalink as follows: http://local.wordpress.test/tournament/2019/my-tournament-name tournament...
I need basic help with custom post types
Basically here's my project: I have to create a back-end wordpress gui user input section. The user will enter details about their projects like name, location, what it is, some other details. A page will display the top 9 recent or so in a 3x3 grid. There will be a search bar to search projects for related tags. I have currently: Background: I am ...
Help me with my first very basic plugin
I am trying to create my first worpress plugin! Actually, the idea is when i click on the button, an ajax request is sent toward php file (ajax-process.php ), it contains a very basic code to pull some data from database and then displaying it as an alert or other in my home page . This is my plugin floder (inside wordpress plugins folder) DB-Pulle...
Basic CSS Help - Removing a shape
I'm trying to disable the triangles above the element labels via css. To view these, press a link, (hear, see, savor) There is dotted line and text. On the text there is a little red triangle/arrow that is not centered.I have done it successfully for the menus, but not the element labels. The website in question is: tedmartinez.com This code above ...
Display several random posts, but make sure a condition is met
I have a custom post type for a group of people (employees). At first, I needed to display 7 random employees. That was easy But now I've been asked to make sure at least one of the seven is always a woman. The man to woman ratio at the company is 4 to 1. I've set a checkbox that I can test for: I need help putting it all together. I assume I need ...
basic mysql/data storage question: information architecture, not as much code itself
I'm trying to figure out my first "more complex" SQL application and am having a bit of a hard time conceptualizing the best way to go about something simple. I understand the code, I think, around various concepts but want to know the best way you think is the way to proceed, and more importantly what terms/how to call this method, so I can do mor...
Also ask