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/>";
}
Related
SELECT distinct(wpnxt.ID),
wpnxt.post_author,
wpnxt.post_date,
wpnxt.post_date_gmt,
wpnxt.post_content,
wpnxt.post_title,
wpnxt.post_excerpt,
wpnxt.post_status,
wpnxt.post_name,
wpnxt.post_modified,
wpnxt.post_modified_gmt,
wpnxt.post_parent,
wpnxt.post_password,
wpnxt.guid,
wpnxt.menu_order,
wpnxt.post_type,
wpnxt.post_mime_type,
wpnxt.comment_count
FROM wp_12_posts AS wpnxt
WHERE wpnxt.post_type = "post"
AND wpnxt.post_status = "publish"
AND wpnxt.ID != 340258
AND wpnxt.post_password = ""
AND EXISTS(SELECT * FROM wp_12_postmeta WHERE (wpnxt.ID = wp_12_postmeta.post_id))
AND EXISTS(SELECT *
FROM wp_12_term_relationships AS tr
WHERE (wp_12_postmeta.post_id = tr.object_id)
AND EXISTS(SELECT *
FROM wp_12_term_taxonomy AS tt
WHERE tt.taxonomy = "category"
AND (tr.term_taxonomy_id = tt.term_taxonomy_id)
AND EXISTS(SELECT *
FROM wp_12_terms t
WHERE tt.term_id = t.term_id AND t.term_id IN (14092, 14092))))
AND meta_key like "product_%_assigned"
ORDER BY wpnxt.post_date DESC
LIMIT 15
Unknown column 'wp_12_postmeta.post_id' in 'where clause' //This is error is shown
The wp_12_postmeta.post_id in your code WHERE (wp_12_postmeta.post_id = tr.object_id) is out of scope .. (the table is not visibile/accesible in this where condition
looking to your code
Instead of several exists on subquery that can't view the related table you could use proper INNER JOIN
SELECT wpnxt.ID,
wpnxt.post_author,
wpnxt.post_date,
wpnxt.post_date_gmt,
wpnxt.post_content,
wpnxt.post_title,
wpnxt.post_excerpt,
wpnxt.post_status,
wpnxt.post_name,
wpnxt.post_modified,
wpnxt.post_modified_gmt,
wpnxt.post_parent,
wpnxt.post_password,
wpnxt.guid,
wpnxt.menu_order,
wpnxt.post_type,
wpnxt.post_mime_type,
wpnxt.comment_count
FROM wp_12_posts AS wpnxt
INNER JOIN wp_12_postmeta ON wpnxt.ID = wp_12_postmeta.post_id
INNER JOIN wp_12_term_relationships tr ON wp_12_postmeta.post_id = tr.object_id
INNER JOIN wp_12_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = "category" ON tt.term_id = t.term_id
AND t.term_id IN (14092, 14092)
INNER JOIN wp_12_terms t ON
WHERE wpnxt.post_type = "post"
AND wpnxt.post_status = "publish"
AND wpnxt.ID != 340258
AND wpnxt.post_password = ""
AND meta_key like "product_%_assigned"
ORDER BY wpnxt.post_date DESC
LIMIT 15
I have a WordPress database that I need to query with pure MySQL to select all posts from a custom taxonomy called 'guide_category'. I also want to order the results by the wp_terms.name and within those order by wp_posts.name. A post can reside within multiple categories.
So results should looks something like:
Category_A
Post A
Post B
Post C
Category_B
Post A
Post B
Post C
Category_C
Post A
Post B
Post C
global $wpdb;
$query = "
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_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = 1
ORDER BY post_date DESC
";
$results = $wpdb->get_results($query);
replace term_taxonomy.term_id = 1 with your texonomy id
may it will work for you
This is what ended up working for me:
SELECT name, post_title
FROM wp_term_taxonomy AS cat_term_taxonomy
INNER JOIN wp_terms AS cat_terms ON cat_term_taxonomy.term_id = cat_terms.term_id
INNER JOIN wp_term_relationships AS cat_term_relationships ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
INNER JOIN wp_posts AS cat_posts ON cat_term_relationships.object_id = cat_posts.ID
INNER JOIN wp_postmeta AS meta ON cat_posts.ID = meta.post_id
WHERE cat_posts.post_status = 'publish'
AND cat_term_taxonomy.taxonomy = 'guide_category'
GROUP BY name, post_title
ORDER BY name, post_title
In looking at the WP_Tax_Query class in /wp-includes/taxonomy.php, I found that there is a 'include_children' option which defaults to true. I modified my original get_posts() call with the following, and it works great:
$pages = get_posts(array(
'post_type' => 'post'
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name'
)
)
));
List of more query parameters: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
I use code below to get data from database
if( !empty( $books_ids ) )
{
$books_ids_in = implode(',', array_fill(0, count($books_ids), '?'));
$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";
$stmt = $conn->prepare($query);
foreach ($books_ids as $k => $id) {
$stmt->bindValue(($k+1), $id);
}
$stmt->execute();
$results = $stmt->fetchAll();
}
and as I use $book id for this purpose, I would like to add some parameter in result to show that, for example, param = "book" for every row. Is there any way to do that?
Just pass it the string and alias it as a column. Though since you know the value passed in in code and display the values though code...... I'm not sure why you need this... as the value is available to you in the code when being displayed.
$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names,
'". $param."' as forEveryRow
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";
this code displays the tags from current category only but, it gets all the tags (hundreds) so, i need to limit the number of returned results and make em random.
How to make this query get only 20 results randomly ?
/* Retrieve all tags from posts in selected categories */
$cats = array('beaches','mountains'); // Must be an array even if only one category
$cats_string = "'" . implode($cats,"','") . "'";
$sql = <<<EOSQL
SELECT DISTINCT t.*
FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id
JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'post_tag')
JOIN $wpdb->terms t ON tt.term_id = t.term_id
WHERE
p.ID IN (
SELECT p2.ID
FROM $wpdb->posts p2
JOIN $wpdb->term_relationships tr2 ON p2.ID = tr2.object_id
JOIN $wpdb->term_taxonomy tt2 ON (tr2.term_taxonomy_id = tt2.term_taxonomy_id AND tt2.taxonomy = 'category')
JOIN $wpdb->terms t2 ON (tt2.term_id = t2.term_id AND t2.name IN ($cats_string))
WHERE p2.post_type = 'post'
AND p2.post_status = 'publish'
AND p2.post_date <= NOW()
)
EOSQL;
$terms = $wpdb->get_results($sql);
// print_r($terms);
echo "<br />";
foreach ($terms as $term) {
echo "ID:$term->term_id NAME:$term->name SLUG:$term->slug<br />";
}
Thanks
You can try an ORDER BY RAND() LIMIT 20, depending on your table size this can run in decent times. See here some details on when to avoid the order by rand() logic. Like suggested, in the specified post, the other approach is to retrieve all the entries and randomly select 20 entries, in PHP rather then using mysql.
In Wordpress, I'm trying to run a query to return a postID where it has been tagged by two (both) 'tags'. I think I may need a subquery of some sort. Any help appreciated, here;s what I have so far.
SELECT wposts .id
FROM wp_posts wposts
INNER JOIN wp_term_relationships ON(wposts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id =wp_term_taxonomy.term_taxonomy_id)
INNER JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id)
WHERE wposts.post_status = 'publish' AND wposts.post_type = 'header-image' AND wp_term_taxonomy.taxonomy = 'tags' AND ( wp_terms.name ='homepage' AND wp_terms.name ='position1' )
SELECT p .id
FROM wp_posts AS p
INNER JOIN wp_term_relationships AS rel1
ON p.ID = rel1.object_id
INNER JOIN wp_term_taxonomy AS tax1
ON rel1.term_taxonomy_id = tax1.term_taxonomy_id
INNER JOIN wp_terms AS term1
ON tax1.term_id = term1.term_id
INNER JOIN wp_term_relationships AS rel2
ON p.ID = rel2.object_id
INNER JOIN wp_term_taxonomy AS tax2
ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
INNER JOIN wp_terms AS term2
ON tax2.term_id = term2.term_id
WHERE p.post_status = 'publish'
AND p.post_type = 'header-image'
AND tax1.taxonomy = 'tags'
AND term1.name ='homepage'
AND tax2.taxonomy = 'tags'
AND term2.name ='position1'
try this:
<?php
// The Query
$the_query = new WP_Query( 'tag=TAG1+TAG2' );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_content();
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
More info here: http://codex.wordpress.org/Class_Reference/WP_Query
And if you are working directly with the database in WP use this: http://codex.wordpress.org/Class_Reference/wpdb