Wordpress get attachment url using wpdb - mysql

How can I modify this query to get the attachment url link to my custom post type.
I have tried several solution from the web but none of them are giving the expected result.
$results = $wpdb->get_results(
"SELECT ID, post_title, {$wpdb->prefix}terms.name, GROUP_CONCAT(meta_value SEPARATOR ', ') AS pictoInfos
FROM {$wpdb->prefix}posts
INNER JOIN {$wpdb->prefix}term_relationships
ON ID = {$wpdb->prefix}term_relationships.object_id
INNER JOIN {$wpdb->prefix}terms
ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}terms.term_id
INNER JOIN {$wpdb->prefix}postmeta
ON ID = {$wpdb->prefix}postmeta.post_id
WHERE (post_type = 'recette' AND post_status = 'publish')
AND (term_taxonomy_id = {$_GET['data']['id']} AND meta_key IN ( 'kilocalorie', 'difficulte', 'nombre_de_minutes' ) )
GROUP BY ID ORDER BY ID DESC ",
ARRAY_A );

Related

Wordpress - MySQL query to show posts only from a specific category

I have the following query that bring the posts that contain a specific word in the title
SELECT posts_post.ID AS post_ID,
posts_post.post_date AS post_post_date,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post
FROM wp_posts AS posts_post
WHERE 1=1
AND posts_post.post_title LIKE '%HOTARAR%'
AND posts_post.post_type = 'post'
GROUP BY post_post_date
The problem now is that I need to bring the posts only from a specific category (tag slug for the category is hotarari-consiliu-local and has the ID 160), how could I modify the above query to bring posts only from a single blog posts category? Thanks!
Try the below query.
global $wpdb;
$make = $wpdb->get_results("
SELECT * FROM
$wpdb->posts
LEFT JOIN
$wpdb->term_relationships
ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN
$wpdb->term_taxonomy
ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.post_type = 'post'
AND
$wpdb->posts.post_title LIKE '%HOTARAR%'
AND
$wpdb->term_taxonomy.taxonomy = 'category'
AND
$wpdb->term_taxonomy.term_id = 160
ORDER BY
post_date DESC
");
Assuming Bhautik is on the right path, then something like this should work (I don't know where 'hotarari-consiliu-local' belongs in this).
SELECT p.ID post_ID
, p.post_date post_post_date
, CONCAT('',p.post_title,'') post_title_with_link_to_post
FROM wp_posts p
JOIN wp_term_relationships pt
ON pt.object_id = p.ID
JOIN wp_term_taxonomy t
ON t.term_taxonomy_id = pt.term_taxonomy_id
WHERE p.post_type = 'post'
AND p.post_title LIKE '%HOTARAR%'
AND t.taxonomy = 'category'
AND t.term_id = 160
ORDER
BY post_date DESC

How to change a query in mysql so it retrieve 30 rows randomly

I'd like to change this query in my WordPress plugin so that it retrieves 30 random rows out of 100.
$query = $wpdb->prepare("
SELECT
p.*, qq.quiz_id, qq.question_order AS order
FROM
{$wpdb->posts} p
INNER JOIN
{$wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id
WHERE
qq.quiz_id IN (" . join( ',', $format ) . ")
AND
p.post_status = %s
", $args );
I've changed it to this but it still does not work. Could any body help me ,please?
SELECT
p.*, qq.quiz_id, qq.question_order AS order
FROM
{$wpdb->posts} p
INNER JOIN
{$wpdb->prefix}learnpress_quiz_questions qq ON p.ID = qq.question_id
WHERE
qq.quiz_id IN (" . join( ',', $format ) . ")
AND
p.post_status = %s
ORDER BY
RAND() LIMIT 30
Change your sort to something like this:
order by RAND() * 30
or if you have an integer id:
order by RAND() * id

Creating sql query that references a meta value

Hoping for some help with a sql query. I have the following:
$sql = "SELECT * from
(SELECT m1.post_id as ID,post_date,m1.meta_value as project_name, m2.meta_value as access_key, m3.meta_value as seq_number
FROM wpha_posts
join wpha_postmeta m1 on m1.post_id = ID and m1.meta_key = '_field_5'
join wpha_postmeta m2 on m2.post_id = ID and m2.meta_key = '_field_7'
join wpha_postmeta m3 on m3.post_id = ID and m3.meta_key = '_seq_num'
where post_type = 'nf_sub' AND post_author = ".$profile_id." AND wpha_postmeta.seq_number = ".$seqNumber.") as v1";
$results = $wpdb->get_results($sql);
What I am trying to do is show data that is from two sql tables but only show results where the sequence number matches the number I have passed via the url.
$seqNumber = (int)$_GET['id'];
I am failing at matching with the meta value of the _seq_num meta key in the database.
Any help would be much appreciated

WordPress posts with featured images and categories

I am looking for a solution for:
SELECT ID
, post_date_gmt
, post_content
, post_title
, post_modified_gmt
FROM wphj_posts
WHERE post_status = 'publish'
and:
SELECT DISTINCT meta_value
, post_id
, meta_id
FROM wphj_postmeta
WHERE meta_key LIKE '_wp_attached_file'
ORDER
BY meta_value ASC
What I did first was this, which doesn't work as I want it to:
SELECT ID
, post_date_gmt
, post_content
, post_title
, post_modified_gmt
, meta_value
FROM wphj_posts
, wphj_postmeta
WHERE post_status = 'publish'
AND meta_key LIKE '_wp_attached_file'
Am simply trying to query WordPress database to get post data from wp_posts and the featured image of the same post from the wp_postmeta table.
Stuff I tried to do: used union, unionall, join on, inner join, left join. Unfortunately they don't work.
Update, came up with this using wpDatatables plugin, works on the plugin dashboard but not as a query on its own :\ - what am i doing wrong folks
SELECT posts_post.ID AS post_ID,
posts_post.post_date_gmt AS post_post_date_gmt,
posts_post.post_title AS post_post_title,
CONCAT('',posts_post.post_title,'') AS post_title_with_link_to_post,
posts_post.post_content AS post_post_content,
posts_post.post_status AS post_post_status,
posts_post.post_name AS post_post_name,
CONCAT(
'<a href="',
posts_post.guid,
'"><img src="',
REPLACE(
posts_post_img.guid,
CONCAT(
'.',
SUBSTRING_INDEX(
posts_post_img.guid,
'.',
-1
)
),
CONCAT(
'-150x150.' ,
SUBSTRING_INDEX(
posts_post_img.guid,
'.',
-1
)
)
),
'" /></a>'
) AS post_thumbnail_with_link_to_post,
post_taxonomy_category_tbl.name AS post_taxonomy_category
FROM wphj_posts AS posts_post
INNER JOIN (SELECT name, object_id as id FROM wphj_terms AS post_taxonomy_category_tbl_terms INNER JOIN wphj_term_taxonomy AS post_taxonomy_category_tbl_termtaxonomy ON post_taxonomy_category_tbl_termtaxonomy.term_id = post_taxonomy_category_tbl_terms.term_id AND post_taxonomy_category_tbl_termtaxonomy.taxonomy = 'category' INNER JOIN wphj_term_relationships AS rel_post_taxonomy_category_tbl ON post_taxonomy_category_tbl_termtaxonomy.term_taxonomy_id = rel_post_taxonomy_category_tbl.term_taxonomy_id) AS post_taxonomy_category_tbl
ON post_taxonomy_category_tbl.ID = posts_post.id
LEFT JOIN (SELECT posts_post_imgposts.guid AS guid, posts_post_imgpostmeta.post_id AS post_id
FROM wphj_postmeta AS posts_post_imgpostmeta
INNER JOIN wphj_posts AS posts_post_imgposts
ON posts_post_imgpostmeta.meta_value = posts_post_imgposts.ID
WHERE posts_post_imgpostmeta.meta_key = '_thumbnail_id' ) AS posts_post_img
ON posts_post_img.post_id = posts_post.ID
WHERE 1=1
AND posts_post.post_type = 'post'
You can try this code, I ran it in sequal pro on on of my local installations, and I got the it to work as I think you would like it to work.
SELECT ID,
post_date_gmt,
post_content,
post_title,
post_modified_gmt,
meta_value
FROM wphj_posts, wphj_postmeta
WHERE wphj_posts.ID = wphj_postmeta.post_id
AND wphj_postmeta.meta_key = '_wp_attached_file'
AND wphj_posts.post_status = 'publish'

Custom query results generate code

Simple question. I have the following custom query. How do I make it generate a html list of the array via "echo?"
$q_result = $wpdb->get_col("SELECT DISTINCT {$wpdb->terms}.name FROM {$wpdb->terms}
INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
WHERE {$wpdb->term_taxonomy}.taxonomy = 'series' AND {$wpdb->term_relationships}.object_id IN (
SELECT object_id FROM {$wpdb->term_relationships}
INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
WHERE {$wpdb->term_taxonomy}.taxonomy = 'media_type' AND {$wpdb->term_taxonomy}.term_id = '16'
ORDER BY {$wpdb->terms}.term_order DESC
) ORDER BY {$wpdb->terms}.term_order ASC");
You can just iterate through the array with a foreach -
foreach( $q_result as $result ){
echo "<a href='/series/".$result."/>".$result."</a>";
}
If you want it to display HTML as well, you will have to echo the proper HTML tags. The above will sort of work for links to the term - the format is TAXONOMY/TERM_SLUG however you are only returning the name, you need to get the name and the slug using get_results() to do it properly.
Reformatted example using table aliases, returning the term and slug:
$sql = <<<SQL
SELECT DISTINCT t.name, t.slug
FROM {$wpdb->terms} t
INNER JOIN {$wpdb->term_taxonomy} tt
ON tt.term_id = t.term_id and tt.taxonomy = 'series'
INNER JOIN {$wpdb->term_relationships} tr
ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id IN (
SELECT object_id
FROM {$wpdb->term_relationships} tr2
INNER JOIN {$wpdb->term_taxonomy} tt2
ON tt2.term_taxonomy_id = tr2.term_taxonomy_id AND tt2.taxonomy = 'media_type' AND tt2.term_id = '16'
ORDER BY {$wpdb->terms}.term_order DESC
)
ORDER BY {$wpdb->terms}.term_order ASC
SQL;
$terms = $wpdb->get_results( $sql );
for ( $terms as $term ){
echo "<a href='/series/{$term->slug}/'>{$term->name}</a><br/>";
}