getting wp posts by tags similar to arg - wp db query
up vote
1
down vote
favorite
I use below code for getting posts by a word similar tag but not working
$query = "
SELECT *
FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
WHERE $wpdb->terms.name LIKE '$search_query%'
AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
ORDER BY $wpdb->posts.post_title";
$search_songs = $wpdb->get_results($query);
$new_query = new WP_Query( $search_songs );
$search_songs_posts = $new_query->posts;
Where is wrong?
mysql sql wordpress search tags
add a comment |
up vote
1
down vote
favorite
I use below code for getting posts by a word similar tag but not working
$query = "
SELECT *
FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
WHERE $wpdb->terms.name LIKE '$search_query%'
AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
ORDER BY $wpdb->posts.post_title";
$search_songs = $wpdb->get_results($query);
$new_query = new WP_Query( $search_songs );
$search_songs_posts = $new_query->posts;
Where is wrong?
mysql sql wordpress search tags
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I use below code for getting posts by a word similar tag but not working
$query = "
SELECT *
FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
WHERE $wpdb->terms.name LIKE '$search_query%'
AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
ORDER BY $wpdb->posts.post_title";
$search_songs = $wpdb->get_results($query);
$new_query = new WP_Query( $search_songs );
$search_songs_posts = $new_query->posts;
Where is wrong?
mysql sql wordpress search tags
I use below code for getting posts by a word similar tag but not working
$query = "
SELECT *
FROM $wpdb->posts , $wpdb->terms , $wpdb->term_relationships
WHERE $wpdb->terms.name LIKE '$search_query%'
AND $wpdb->terms.term_id = $wpdb->term_relationships.term_taxonomy_id
AND $wpdb->term_relationship.object_id = $wpdb->posts.ID
ORDER BY $wpdb->posts.post_title";
$search_songs = $wpdb->get_results($query);
$new_query = new WP_Query( $search_songs );
$search_songs_posts = $new_query->posts;
Where is wrong?
mysql sql wordpress search tags
mysql sql wordpress search tags
edited Nov 19 at 7:34
Gufran Hasan
3,26831325
3,26831325
asked Nov 19 at 7:23
ebeliejinfren
156
156
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
As I have seen your first query looks fine.
$query_byTag="
SELECT wp_posts.ID
FROM wp_posts, wp_term_relationships, wp_terms
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
AND wp_terms.name LIKE '".$search_query."%'";
$search_songs = $wpdb->get_results($query_byTag);
This query returns all posts IDs as an array of object.
Array
(
[0] => stdClass Object
(
[ID] => 1
)
[1] => stdClass Object
(
[ID] => 60
)
)
Now you need to pass $search_songs
array of object WP_Query($search_songs)
as:
$new_query = new WP_Query( $search_songs );
echo '<pre>';
print_r($search_songs);//returns all posts with query and others data
echo '</pre>';
If you want to get only posts then you can get it as:
echo '<pre>';
print_r($search_songs->posts);//returns all posts
echo '</pre>';
For more details
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
do one thingprint_r($search_songs)
.
– Gufran Hasan
Nov 19 at 7:56
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
As I have seen your first query looks fine.
$query_byTag="
SELECT wp_posts.ID
FROM wp_posts, wp_term_relationships, wp_terms
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
AND wp_terms.name LIKE '".$search_query."%'";
$search_songs = $wpdb->get_results($query_byTag);
This query returns all posts IDs as an array of object.
Array
(
[0] => stdClass Object
(
[ID] => 1
)
[1] => stdClass Object
(
[ID] => 60
)
)
Now you need to pass $search_songs
array of object WP_Query($search_songs)
as:
$new_query = new WP_Query( $search_songs );
echo '<pre>';
print_r($search_songs);//returns all posts with query and others data
echo '</pre>';
If you want to get only posts then you can get it as:
echo '<pre>';
print_r($search_songs->posts);//returns all posts
echo '</pre>';
For more details
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
do one thingprint_r($search_songs)
.
– Gufran Hasan
Nov 19 at 7:56
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
|
show 2 more comments
up vote
1
down vote
accepted
As I have seen your first query looks fine.
$query_byTag="
SELECT wp_posts.ID
FROM wp_posts, wp_term_relationships, wp_terms
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
AND wp_terms.name LIKE '".$search_query."%'";
$search_songs = $wpdb->get_results($query_byTag);
This query returns all posts IDs as an array of object.
Array
(
[0] => stdClass Object
(
[ID] => 1
)
[1] => stdClass Object
(
[ID] => 60
)
)
Now you need to pass $search_songs
array of object WP_Query($search_songs)
as:
$new_query = new WP_Query( $search_songs );
echo '<pre>';
print_r($search_songs);//returns all posts with query and others data
echo '</pre>';
If you want to get only posts then you can get it as:
echo '<pre>';
print_r($search_songs->posts);//returns all posts
echo '</pre>';
For more details
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
do one thingprint_r($search_songs)
.
– Gufran Hasan
Nov 19 at 7:56
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
|
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As I have seen your first query looks fine.
$query_byTag="
SELECT wp_posts.ID
FROM wp_posts, wp_term_relationships, wp_terms
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
AND wp_terms.name LIKE '".$search_query."%'";
$search_songs = $wpdb->get_results($query_byTag);
This query returns all posts IDs as an array of object.
Array
(
[0] => stdClass Object
(
[ID] => 1
)
[1] => stdClass Object
(
[ID] => 60
)
)
Now you need to pass $search_songs
array of object WP_Query($search_songs)
as:
$new_query = new WP_Query( $search_songs );
echo '<pre>';
print_r($search_songs);//returns all posts with query and others data
echo '</pre>';
If you want to get only posts then you can get it as:
echo '<pre>';
print_r($search_songs->posts);//returns all posts
echo '</pre>';
For more details
As I have seen your first query looks fine.
$query_byTag="
SELECT wp_posts.ID
FROM wp_posts, wp_term_relationships, wp_terms
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_relationships.term_taxonomy_id
AND wp_terms.name LIKE '".$search_query."%'";
$search_songs = $wpdb->get_results($query_byTag);
This query returns all posts IDs as an array of object.
Array
(
[0] => stdClass Object
(
[ID] => 1
)
[1] => stdClass Object
(
[ID] => 60
)
)
Now you need to pass $search_songs
array of object WP_Query($search_songs)
as:
$new_query = new WP_Query( $search_songs );
echo '<pre>';
print_r($search_songs);//returns all posts with query and others data
echo '</pre>';
If you want to get only posts then you can get it as:
echo '<pre>';
print_r($search_songs->posts);//returns all posts
echo '</pre>';
For more details
edited Nov 19 at 12:45
answered Nov 19 at 7:46
Gufran Hasan
3,26831325
3,26831325
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
do one thingprint_r($search_songs)
.
– Gufran Hasan
Nov 19 at 7:56
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
|
show 2 more comments
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
do one thingprint_r($search_songs)
.
– Gufran Hasan
Nov 19 at 7:56
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
i don't understand, what's problem ? why need loop and where exactly ?
– ebeliejinfren
Nov 19 at 7:55
do one thing
print_r($search_songs)
.– Gufran Hasan
Nov 19 at 7:56
do one thing
print_r($search_songs)
.– Gufran Hasan
Nov 19 at 7:56
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
i know, but don't return any things
– ebeliejinfren
Nov 19 at 11:48
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
Are you getting any error?
– Gufran Hasan
Nov 19 at 12:26
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
@ebeliejinfren, I have updated my answer and also tested it, now It's working.
– Gufran Hasan
Nov 19 at 12:44
|
show 2 more comments
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%2f53370014%2fgetting-wp-posts-by-tags-similar-to-arg-wp-db-query%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