I've been trying to find a way (armed with only a basic knowledge of php and a wildly optimistic sense of determination) to search a wordpress database for meta-values based on other meta-values.
I need to get an item name from one table (wp_posts) using the post's ID, use it to obtain the post_id of items with the same name in another table (wp_postmeta) and then get from the same table the values for a particular custom field for every post_id obtained.
Since I'm new to this I attempted to do it by breaking it down into parts and here's what I've got.
<!-- This part works and gets the post title as expected -->
<?php
$result1 =$wpdb->get_results("SELECT post_title FROM $wpdb->posts , $wpdb->postmeta where $wpdb->posts.ID = $post->ID", OBJECT);
print_r($result1[0]->post_title);
?>
<!-- Then, I expected this to return the post_id of all entries where the title is the same as the one just obtained and where meta_key = custom_field_one. But it doesn't -->
<?php
$result2 =$wpdb->get_results("SELECT post_id FROM $wpdb->posts , $wpdb->postmeta where $wpdb->postmeta.meta_value = $result1 AND $wpdb->postmeta.meta_key = custom_field_one", OBJECT );
print_r($result2->post_id);
?>
<!-- So, unsurprisingly, this part doesn't work either. But it should use every post_id it just obtained to once again search the postmeta table -->
<?php
$result3 =$wpdb->get_results("SELECT meta_value FROM $wpdb->posts , $wpdb->postmeta where $wpdb->postmeta.post_id = $result2 AND $wpdb->postmeta.meta_key = custom_field_two", OBJECT );
print_r($result3->meta_value);
?>
Since the first query works, and the subsequent ones are just slight variations on that I expected it all to work. But, well, I was wrong.
Incidentally, I've tried a number of syntax variations in case that was an issue and I'm aware there are some issues regarding arrays. So any advice would be appreciated.
Use aliases:
SELECT
a1.Name,
b1.Info
FROM table2 b1
JOIN table2 a1 ON b1.id= a1.id AND a1.status = 1
Related
I'm doing a custom search in my WP and in the search field, any typed word will be sought in custom-fields, in the titles and posting content.
It turns out that my database has more than 1 million custom-fields lines, and wanted to limit the search only for a certain type of post.
I already do this in the Loop to display the results, but I wish this filter was made in consultation because the page is taking over 20 seconds to display the result.
Can anyone help me? Below the code I'm using.
// SEARCH FROM ALL CUSTOM FIELDS
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
ALTER TABLE wp_posts ADD INDEX (postmeta)
SELECT DISTINCT post_id FROM {$wpdb->postmeta}
WHERE meta_value LIKE '%s'
", $keyword ) );
// SEARCH FOR TITLE AND CONTENT
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
ALTER TABLE wp_post s ADD INDEX (posts);
SELECT DISTINCT ID FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
OR post_content LIKE '%s'
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );
UPDATE:
Try this one. It's not tested but I'm sure it will work if you check and fix it a little bit:
// SEARCH IN title, content and meta_value
$post_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT ID FROM {$wpdb->posts} AS p,
LEFT JOIN {$wpdb->postmeta} AS m,
ON m.post_id = p.ID
WHERE p.post_type = '%s'
AND (
post_title LIKE '%%s%'
OR m.meta_value LIKE '%%s%'
OR post_content LIKE '%%s%')
", $post_type, $keyword, $keyword, $keyword) );
Note:
Assume you have taken input $post_type & $keyword. Eg: $post_type = "fairs";
I added % before and after %s to make sure it's widecard search.
In this case, I use DISTINCT because the joint table have repeated IDs. My apologies for the previous fault assumption on your first query that it was not needed.
Speed is not guaranteed, since you are doing a widecard search on post_content which is not indexed.
You should make sure the meta_value field is indexed for a little faster speed. If you meta don't store too much text in it.
GOOD LUCK!
ORIGINAL:
Firstly, let's clarify these:
Remove these 2 lines as it won't help (it make it worse due to repeated call):
ALTER TABLE wp_posts ADD INDEX (postmeta)
And
ALTER TABLE wp_posts ADD INDEX (posts);
ID is already distinct. So it won't help you do: SELECT DISTINCT ID
The final queries should look like these:
// SEARCH FROM ALL CUSTOM FIELDS
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
SELECT post_id FROM {$wpdb->postmeta}
WHERE meta_value LIKE '%s'
", $keyword ) );
// SEARCH FOR TITLE AND CONTENT
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
SELECT ID FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
OR post_content LIKE '%s' // Remove this will boost performance by 90% because post_content is not indexed
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );
What I recommend is to use this plugin: https://wordpress.org/plugins/search-everything/
Assuming that $keyword has no wildcards, you might try expressing this query as:
SELECT ID
FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
UNION
SELECT ID
FROM {$wpdb->posts}
WHERE post_content LIKE '%s';
MySQL might figure out to use the indexes for this. The indexes that you want are:
create index wp_post_title_id on wp_post(post_title, id);
create index wp_post_content_id on wp_post(post_content, id);
This assumes that{$wpdb->posts} is really wp_post.
I use the following SQL query to fetch custom posts in Wordpress.
I ended up with this solution because i needed to fetch und sort the posts based on custom meta data. This woks pretty well so far!
But how do i incorporate custom taxonomies in my query?
Say for example only fetch posts that are associated with term_id 5?
I'm really stuck here cause i can't figure out how wp_terms, relations etc are connected...
Any help is greatly appreciated!
UPDATE:
After some reading the solution in my case is this:
WordPress stores the relations between taxonomies and posts in wp_term_relationships (where object_id is the post_id and term_taxonomy_id ist the term_id in wp_terms). So if i want to only fetch posts that belong to a specific term_id, i came up with the following query. Seems to work as far as i can tell!
This is my query
global $wpdb;
$activelang = ICL_LANGUAGE_CODE;
$cmonth = date('Y-m');
$myquery = "
SELECT wposts.*, wpostmeta.meta_value AS date
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_icl_translations wicl_translations, , wp_term_relationships wptermrelations
WHERE wposts.post_status = 'publish'
AND wpostmeta.post_id = wposts.ID
AND (wptermrelations.object_id = wposts.ID AND (wptermrelations.term_taxonomy_id = '29' OR wptermrelations.term_taxonomy_id = '30'))
AND wposts.post_type = 'event'
AND wicl_translations.element_id = wposts.ID
AND wicl_translations.language_code = '$activelang'
AND (wpostmeta.meta_key = '_ws_prem_date' OR wpostmeta.meta_key = '_ws_date1_date' OR ... ... OR wpostmeta.meta_key = '_ws_date10_date')
AND wpostmeta.meta_value >= '$cmonth-01'
ORDER BY CAST(date AS DATETIME),wposts.post_title ASC
";
$myloop = $wpdb->get_results($myquery);
In case someone is wondering what this query does:
This query fetches custom post types (events that have multiple dates associated) and builds a loop for these events that allows for duplicate entries since a 'normal' WordPress loop always filters out any duplicate entries.
I have been trying to fetch posts from a wordpress site's mysql db to my own script for a few days.
<?php
$querystr = "
SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'tag'
AND $wpdb->postmeta.meta_value = 'email'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
I can see thumbnail images on the original site but I could not figure out where to get that image location data in the db.
What would be the enhancements on my query in order to have image information with current ones?
You need to dive into your wp_postmeta table. This is where you will find everything. You can get the thumbnail ID with the following query:
SELECT * FROM 'wp_postmeta' WHERE post_id=**ID_HERE** AND meta_key='_thumbnail_id'
You will get a thumbnail ID from which you will be able to get your real thumbnail with the following query:
SELECT * FROM 'wp_postmeta' WHERE post_id=**PREVIOUSLY_OBTAINED_ID**
This will actually give you two rows :
One for the URL of your image (meta_key="_wp_attached_file")
One for the image info like its dimensions, its title etc... (meta_key="_wp_attachment_metadata")
Try this code:
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
More details here: http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
I’ve a small piece of code I’ve tried to wrap my brain around for some hours now.
I’m trying to create a query which retrieves some elements and in the end group them by a selector.
I’ll try to demonstrate: I’m querying all the members in my organization. Each member is a member of a “department” and there can be several members in each department, but only one department for each member.
So I’ve created the query to select all members in my organization group by departments.
When I’m displaying it using a simple foreach($my_query as $q) … I want to output $q->department_name but only once for that department. Right now it says “Marketing” under all five members and then it changes to “HR” for all those members. I’ve tried some different methods such as array_unique, creating a function that checks for the string to see if it is the same and other but with no result.
I could create two foreach loops but if it is at all possible not to I would prefer that because two foreach loops would affect the performance.
Any help or suggestion would be very much appreciated.
Sinerely
- Mestika
--- Edited ---
By the way, my query looks like this:
SELECT *
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status = 'publish'
AND meta.meta_key = 'active'
AND meta.meta_value = 'active'
AND cat_posts.post_type = 'member'
AND cat_term_taxonomy.taxonomy = 'deparment'
GROUP BY cat_terms.slug, cat_term_relationships.object_id
$prev_department = NULL;
foreach ($my_query AS $q)
{
if ($prev_department != $q->department_name)
echo '<h2>'. htmlentities($q->department_name, ENT_COMPAT, 'UTF-8') .'</h2>';
// ...
$prev_department = $q->department_name;
}
I have this query:
$query = "SELECT *FROM wp_postmeta WHERE meta_key = '_isEvent' AND meta_value ='yes' ORDER BY post_id LIMIT 0, 5" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$eventid = $row ['post_id'];
echo "<p>".$eventid."</p>";
}
This currently posts some ids $eventid. I now want to run another query in another table (same database) that pulls some post titles that matches those id's.
Table is called "wp_posts" and column to match is "ID" and want to echo out post title from "post_title".
Where do I start?
Sounds like you want to use SQL joins. W3 schools has a good intro article about this.
http://www.w3schools.com/sql/sql_join.asp
Something like the following
SELECT post_title FROM wp_postmeta m, wp_posts p WHERE m.wmeta_key = '_isEvent' AND m.meta_value ='yes' AND m.post_id == p.post_idORDER BY m.post_id LIMIT 0, 5