I am writing some custom code for wordpress using the postmeta table. The items are stored in here and the common pk1 is the post_id.
However what I am doing will have a bunch of clickable parts of the postmeta such as
Or something similar. Now the table will hold multiple post data for different post that have the same source_site or source_author.
A user can then click on the source_author and they will be taken to a new table that contains all of the posts or articles that link back to this source_author.
The issue is that the I am linking to a postmeta value rather than via the pk1 of the post-id.
This means to get the rest of the data I am going to either have to do an additional query or do a join into the same table.
This brings me to my question. Would it be better to do the join or the subquery.
The further problem is that the postmeta table stores all the various data and keys and values, so it makes it hard to query the other parts I want.
so the table would look something like
|meta_id|post_id|meta_key|meta_value|
|1 | 2 |source_author | Dan Holmes |
|2 | 2 |source_site |http://somesite.com|
Just looking for the cleanest way to grab this data, all I can think otherwise is to grab the data first for the author with:
$the_post_ids = select post_id from postmeta where meta_key='source_author' and meta_value='Dan Holmes'
foreach($the_post_ids as $post_id) {
select * from postmeta where post_id=$post_id;
}
now that is all very much pseudo code and i guess the problem is that once the data starts to get fairly large, I am concerned that this will end up with a lot of overheads for each section that can be clicked on and the fact i have to do multiple selects into a single array.
Is there a better way to handle this?
As you mention, performance becomes a concern as the database grows, so you'll want to limit the round-trips to the database to as few as possible. I'd pull back all the meta-table rows for the given author like this query:
SELECT * FROM postmeta WHERE post_id IN
(SELECT post_id FROM postmeta
WHERE meta_key='source_author' AND meta_value='Dan Holmes'
)
Then write a foreach loop in PHP similar to the one you have, to handle the results.
EDIT
Here's a different option, without using the IN clause
SELECT meta1.* FROM postmeta meta1
INNER JOIN
(SELECT DISTINCT postid FROM postmeta
WHERE postmeta.meta_key = 'source_author' AND postmeta.meta_value = 'Dan Holmes'
) meta2
ON meta1.post_id = meta2.post_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.