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:




  1. I want make a query to the posts table in the database and get all the posts which are published.

  2. 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









share|improve this question
























  • please add schema and sample data of both the tables.
    – new_user
    21 hours ago










  • added the sample data
    – mrdeath4
    20 hours ago















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:




  1. I want make a query to the posts table in the database and get all the posts which are published.

  2. 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









share|improve this question
























  • please add schema and sample data of both the tables.
    – new_user
    21 hours ago










  • added the sample data
    – mrdeath4
    20 hours ago













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:




  1. I want make a query to the posts table in the database and get all the posts which are published.

  2. 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









share|improve this question















I am using a wordpress website and a custom PHP srcipt inside, which will run with CRON to update posts.



How I see it:




  1. I want make a query to the posts table in the database and get all the posts which are published.

  2. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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'





share|improve this answer























  • 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 field post_title, id, meta_key
    – Sadikhasan
    19 hours ago










  • it gives an empty result and without any errors
    – mrdeath4
    19 hours ago


















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.






share|improve this answer





















  • 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


















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'





share|improve this answer





















  • it finds nothing too ;(
    – mrdeath4
    18 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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'





share|improve this answer























  • 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 field post_title, id, meta_key
    – Sadikhasan
    19 hours ago










  • it gives an empty result and without any errors
    – mrdeath4
    19 hours ago















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'





share|improve this answer























  • 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 field post_title, id, meta_key
    – Sadikhasan
    19 hours ago










  • it gives an empty result and without any errors
    – mrdeath4
    19 hours ago













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'





share|improve this answer














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'






share|improve this answer














share|improve this answer



share|improve this answer








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 field post_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






  • 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 field post_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












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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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
















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










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'





share|improve this answer





















  • it finds nothing too ;(
    – mrdeath4
    18 hours ago















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'





share|improve this answer





















  • it finds nothing too ;(
    – mrdeath4
    18 hours ago













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'





share|improve this answer












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'






share|improve this answer












share|improve this answer



share|improve this answer










answered 19 hours ago









Polvonjon

673




673












  • it finds nothing too ;(
    – mrdeath4
    18 hours ago


















  • it finds nothing too ;(
    – mrdeath4
    18 hours ago
















it finds nothing too ;(
– mrdeath4
18 hours ago




it finds nothing too ;(
– mrdeath4
18 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Saint-Aignan (Tarn-et-Garonne)

Volksrepublik China

How to test boost logger output in unit testing?