I am using a wordpress website and a custom PHP srcipt inside, which will run with CRON to update posts.
How I see it:
- I want make a query to the posts table in the database and get all the posts which are published.
- I need to make one more query to the table - postmeta - to get the value from my custom field (there is a link I need to parse)
How I do it:
$pages = $wpdb->get_results(
"
SELECT post_title, id
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_type = 'post'
"
);
if( $pages ) {
foreach ( $pages as $page ) {
echo $page->post_title . " - ";
echo $page->id . "<br>";
}
}
So the question is: The problem is with the MySQL query. I need this response: array[0] -> ID (from posts), post_title (from posts), meta_value (from postmeta where meta_key = 'src_link'). How I can get this response?
I tried this - but id doesnot work:
SELECT post_title, id, meta_value
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id
WHERE post.post_status = 'publish'
AND post.post_type = 'post'
AND meta.meta_key='src_link'
The problem is when I add this line -
AND meta.meta_key='src_link'
It don't find anything. If I delete this line. It finds all I need but with dupclicates ( I need only rows where Meta_key = 'src_link'.
The tables:
posts:
-------------------
id | post_title
------------------
1 | new title here
------------------
2 | again a title here
postmeta:
meta_id | post_id | meta_key | meta_value
---------------------------------------------
1 | 2 | src_link | here_is_my_link
---------------------------------------------
2 | 1 | empty | not_my_link