double MySQL query in one query in PHP wordpress
up vote
3
down vote
favorite
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
php mysql wordpress
add a comment |
up vote
3
down vote
favorite
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
php mysql wordpress
please add schema and sample data of both the tables.
– new_user
21 hours ago
added the sample data
– mrdeath4
20 hours ago
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
php mysql wordpress
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
php mysql wordpress
php mysql wordpress
edited 16 hours ago
asked 21 hours ago
mrdeath4
6719
6719
please add schema and sample data of both the tables.
– new_user
21 hours ago
added the sample data
– mrdeath4
20 hours ago
add a comment |
please add schema and sample data of both the tables.
– new_user
21 hours ago
added the sample data
– mrdeath4
20 hours ago
please add schema and sample data of both the tables.
– new_user
21 hours ago
please add schema and sample data of both the tables.
– new_user
21 hours ago
added the sample data
– mrdeath4
20 hours ago
added the sample data
– mrdeath4
20 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
You can use INNER JOIN like
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id
WHERE post_status = 'publish' and meta.meta_key='src_link'
AND post_type = 'post'
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
2
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
Try my updated answer do not forgot to take fieldpost_title, id, meta_key
– Sadikhasan
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
|
show 2 more comments
up vote
0
down vote
As you're using the global $wpdb
we can assume your PHP is within the WP framework. As such, you shouldn't need a custom SQL query - you can do this with a standard WP_Query:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'src_link',
'compare' => 'EXISTS',
),
),
);
$postsQuery = get_posts($args);
foreach ($postsQuery as $myPost) {
// We're using $myPost rather than $post as the latter is a global var used in The Loop
echo '<pre>' . print_r($myPost, true) . '</pre>';
update_post_meta($myPost->ID, 'updated_link', $myCustomValue);
}
Here we're fetching all post
-type posts with a status of publish
and using a meta_query
to find posts where src_link
exists.
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely themeta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (anypost
-type posts with themeta_key
ofsrc_link
).
– indextwo
14 hours ago
add a comment |
up vote
0
down vote
Try this:
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id and meta.meta_key='src_link'
WHERE post_status = 'publish'
AND post_type = 'post'
it finds nothing too ;(
– mrdeath4
18 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can use INNER JOIN like
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id
WHERE post_status = 'publish' and meta.meta_key='src_link'
AND post_type = 'post'
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
2
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
Try my updated answer do not forgot to take fieldpost_title, id, meta_key
– Sadikhasan
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
|
show 2 more comments
up vote
1
down vote
accepted
You can use INNER JOIN like
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id
WHERE post_status = 'publish' and meta.meta_key='src_link'
AND post_type = 'post'
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
2
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
Try my updated answer do not forgot to take fieldpost_title, id, meta_key
– Sadikhasan
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
|
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can use INNER JOIN like
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id
WHERE post_status = 'publish' and meta.meta_key='src_link'
AND post_type = 'post'
You can use INNER JOIN like
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id
WHERE post_status = 'publish' and meta.meta_key='src_link'
AND post_type = 'post'
edited 19 hours ago
answered 21 hours ago
Sadikhasan
13.7k125891
13.7k125891
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
2
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
Try my updated answer do not forgot to take fieldpost_title, id, meta_key
– Sadikhasan
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
|
show 2 more comments
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
2
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
Try my updated answer do not forgot to take fieldpost_title, id, meta_key
– Sadikhasan
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
thank you for fast anwering but this code - added 5 duplicates and didnot found the src-link
– mrdeath4
21 hours ago
2
2
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
Please update question with your sample data so I can help you as per your expected output.
– Sadikhasan
21 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
updated the question - and added some sample data
– mrdeath4
20 hours ago
Try my updated answer do not forgot to take field
post_title, id, meta_key
– Sadikhasan
19 hours ago
Try my updated answer do not forgot to take field
post_title, id, meta_key
– Sadikhasan
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
it gives an empty result and without any errors
– mrdeath4
19 hours ago
|
show 2 more comments
up vote
0
down vote
As you're using the global $wpdb
we can assume your PHP is within the WP framework. As such, you shouldn't need a custom SQL query - you can do this with a standard WP_Query:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'src_link',
'compare' => 'EXISTS',
),
),
);
$postsQuery = get_posts($args);
foreach ($postsQuery as $myPost) {
// We're using $myPost rather than $post as the latter is a global var used in The Loop
echo '<pre>' . print_r($myPost, true) . '</pre>';
update_post_meta($myPost->ID, 'updated_link', $myCustomValue);
}
Here we're fetching all post
-type posts with a status of publish
and using a meta_query
to find posts where src_link
exists.
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely themeta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (anypost
-type posts with themeta_key
ofsrc_link
).
– indextwo
14 hours ago
add a comment |
up vote
0
down vote
As you're using the global $wpdb
we can assume your PHP is within the WP framework. As such, you shouldn't need a custom SQL query - you can do this with a standard WP_Query:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'src_link',
'compare' => 'EXISTS',
),
),
);
$postsQuery = get_posts($args);
foreach ($postsQuery as $myPost) {
// We're using $myPost rather than $post as the latter is a global var used in The Loop
echo '<pre>' . print_r($myPost, true) . '</pre>';
update_post_meta($myPost->ID, 'updated_link', $myCustomValue);
}
Here we're fetching all post
-type posts with a status of publish
and using a meta_query
to find posts where src_link
exists.
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely themeta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (anypost
-type posts with themeta_key
ofsrc_link
).
– indextwo
14 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
As you're using the global $wpdb
we can assume your PHP is within the WP framework. As such, you shouldn't need a custom SQL query - you can do this with a standard WP_Query:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'src_link',
'compare' => 'EXISTS',
),
),
);
$postsQuery = get_posts($args);
foreach ($postsQuery as $myPost) {
// We're using $myPost rather than $post as the latter is a global var used in The Loop
echo '<pre>' . print_r($myPost, true) . '</pre>';
update_post_meta($myPost->ID, 'updated_link', $myCustomValue);
}
Here we're fetching all post
-type posts with a status of publish
and using a meta_query
to find posts where src_link
exists.
As you're using the global $wpdb
we can assume your PHP is within the WP framework. As such, you shouldn't need a custom SQL query - you can do this with a standard WP_Query:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'src_link',
'compare' => 'EXISTS',
),
),
);
$postsQuery = get_posts($args);
foreach ($postsQuery as $myPost) {
// We're using $myPost rather than $post as the latter is a global var used in The Loop
echo '<pre>' . print_r($myPost, true) . '</pre>';
update_post_meta($myPost->ID, 'updated_link', $myCustomValue);
}
Here we're fetching all post
-type posts with a status of publish
and using a meta_query
to find posts where src_link
exists.
answered 19 hours ago
indextwo
2,17633246
2,17633246
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely themeta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (anypost
-type posts with themeta_key
ofsrc_link
).
– indextwo
14 hours ago
add a comment |
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely themeta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (anypost
-type posts with themeta_key
ofsrc_link
).
– indextwo
14 hours ago
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
THank you but I copypasted this code and it doesnot fins any posts
– mrdeath4
16 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely the
meta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (any post
-type posts with the meta_key
of src_link
).– indextwo
14 hours ago
@mrdeath4 That code definitely works - I tested it with a couple of different keys on a handful of installations and it returns posts in all cases. As such, I would assume that most likely the
meta_key
you're searching for doesn't exist. Check your database values and make sure that the things you're searching for definitely exist (any post
-type posts with the meta_key
of src_link
).– indextwo
14 hours ago
add a comment |
up vote
0
down vote
Try this:
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id and meta.meta_key='src_link'
WHERE post_status = 'publish'
AND post_type = 'post'
it finds nothing too ;(
– mrdeath4
18 hours ago
add a comment |
up vote
0
down vote
Try this:
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id and meta.meta_key='src_link'
WHERE post_status = 'publish'
AND post_type = 'post'
it finds nothing too ;(
– mrdeath4
18 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Try this:
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id and meta.meta_key='src_link'
WHERE post_status = 'publish'
AND post_type = 'post'
Try this:
SELECT post_title, id, meta_key
FROM $wpdb->posts as post
INNER JOIN $wpdb->postmeta as meta ON post.id=meta.post_id and meta.meta_key='src_link'
WHERE post_status = 'publish'
AND post_type = 'post'
answered 19 hours ago
Polvonjon
673
673
it finds nothing too ;(
– mrdeath4
18 hours ago
add a comment |
it finds nothing too ;(
– mrdeath4
18 hours ago
it finds nothing too ;(
– mrdeath4
18 hours ago
it finds nothing too ;(
– mrdeath4
18 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371065%2fdouble-mysql-query-in-one-query-in-php-wordpress%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please add schema and sample data of both the tables.
– new_user
21 hours ago
added the sample data
– mrdeath4
20 hours ago