Wordpress custom SELECT query for alphabetical index - wordpress

I'm trying to make an alphabetical index with wordpress, so far I've managed to get the $letter parameter and make the query, but I can't sort the results by a specific category_name, here's my code:
global $wpdb;
$results = $wpdb->get_results(
"
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$letter%'
AND post_type = 'directory'
AND category_name = 'company';
"
);
Everything works just fine untill the query gets to the category_name, I checked the Wordpress Codex but I didn't get it very well.
I would appreciate any help, thanks!

Related

Get the latest order ID after placed order in woocommerce

I want get the last order ID done after the placed order. I have used this for get the last order id.
global $wpdb;
$results = $wpdb->get_results( ' SELECT * FROM `wp_woocommerce_order_items`
ORDER BY `wp_woocommerce_order_items`.`order_item_id` DESC
LIMIT 1', OBJECT );
But apart from this, is there any other woocommerce function or hook to get the last order ID.
Thanks in advance.
If you just placed an order and nothing else then you can use ' $wpdb->insert_id'.. it will do the trick.

Wordpress - showing last uploaded images, but only from a specific custom post type

I have a Wordpress-powered website with several custom post types, and I'm struggling to find a way to load the latest uploaded images from one of the custom post types only.
In other words, I know how to ask for the most recent 4 images added to the Wordpress media library, but I can't find a way to filter the attachments depending of the post type of their parent page (in this case, show only the images uploaded to 'Image Gallery' posts - not regular posts, not 'Video Gallery' posts, etc).
It feels like a very basic question, but for some reason I can't get this work and I'm really frustrated. Is there any way to get the post type of the post parent in the query? Or I should approach this issue from another angle?
I don't believe it's possible to use WP_Query to filter based on the post type of the parent. The most straightforward way around this is to use SQL and $wpdb->get_results() directly. This will also bypass all built in caching, etc so think about how your implementation will be used.
global $wpdb;
// the SQL statement to only fetch the N last attachments
$sql = <<<SQL
SELECT attachments.*
FROM {$wpdb->posts} attachments
-- join the parent post based on the parent_post value to filter by cpt
JOIN {$wpdb->posts} post
ON post.ID = attachments.post_parent
AND post.post_status = 'publish'
-- pass in the custom post type
AND post.post_type = %s
WHERE
-- only fetch images, double percents for prepare()
attachments.post_mime_type LIKE 'image%%'
ORDER BY attachments.post_date DESC
-- pass in the number to fetch
LIMIT %d
SQL;
$cpt = "your_custom_post_type";
$limit = 4;
$attachments = $wpdb->get_results( $wpdb->prepare( $sql, $cpt, $limit ) );
foreach ( $attachments as $attachment ){
// $attachment will have all the rows from the posts table.
$id = $attachment->ID;
}

Wordpress wpdb query issue - how to filter out revisions?

Can someone tell me how to modify the code below so that it filters out revisions? I tried using revision deletion plugins but they only work for revisions of posts or pages... not custom post types.
<?php
global $wpdb;
global $post;
$review_id = $post->ID;
if (get_post_type($review_id) == "features") {
$assoc_id = get_post_meta($review_id, "associated_hosts_value", true);
$review_id = intval($assoc_id);
}
$assoc = $wpdb->get_col("SELECT DISTINCT post_id from wp_postmeta WHERE (meta_key='associated_hosts_value') AND ((meta_value LIKE ',{$review_id},%') OR (meta_value LIKE '%,{$review_id},%') OR (meta_value = '{$review_id}') OR (meta_value LIKE '%,{$review_id}'));");
Revision is stored in wp_posts under the post_type column, so you will need to inner join on wp_posts and query for a post_type NOT IN ('revision'):
"SELECT DISTINCT post_id from {$wpdb->postmeta} INNER JOIN {$wpdb->posts} ON ID = post_id WHERE post_type NOT IN ('revision') ..."
By the way, inserting raw SQL queries in to get_col is a bad practice, as it leaves you vulnerable to SQL injections. Use $wpdb->prepare() as shown here:
http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
Note that you will need to escape the % used in the LIKE statements by changing them to %% to work within prepare().
Also, you shouldn't hard code WP table names in to your query statements. The wpdb class stores table names for use in constructing your queries, i.e. $wpdb->posts, $wpdb->postmeta. Using these assures that your plugin will always query the correct WP tables. Try your code in a multi site installation to find out why this is important.

Display number of posts from a certain category?

How can I display ONLY the number of posts from a certain category ?
I want to display only the number without the category name.
I used this code
wp_list_categories('show_count=1&include=23&title_li=');
but it displays the category.
Any help please ??
I think it would help you to read this => http://wordpress.org/support/topic/display-number-of-posts-per-category
function number_postpercat($idcat) {
global $wpdb;
$query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
$num = $wpdb->get_col($query);
echo $num[0];
}

MYSQL Statement INNER JOIN with multiple condition

I have a WP site where I've also got a few php pages outside of my WP installation which I need to query the database that powers my WP site. I have a working query but it doesn't show what I really need output.
These are the two tables wp_posts and wp_postmeta
wp_posts
ID
post_author
post_date
post_date_gmt
post_content
post_title
post_excerpt
post_status
comment_status
pint_status
post_password
post_name
to_ping
pinged
post_modified
post_modified_gmt
post_content_filtered
post_parent
guid
menu_order
post_type
post_mine_type
comment_count
wp_postmeta
meta_id
post_id
meta_key
meta_value
and these is the query that show the publish post:
$query = "SELECT post_title, post_name FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5";
and these is the query that show with the meta_key= '_wp_attached_file'
$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' ";
what I want is to combine these two query to show post_status ='publish' and the meta_key = '_wp_attached_file'
here is the example of my query but doesn't show value
<?php
$query ="SELECT post.ID, post.post_title, post.post_excerpt, post.post_content, meta.meta_value FROM wp_posts AS post INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id AND meta.meta_key = '_wp_attached_file' WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5 ";
$result = mysql_query($query,$link);
if(mysql_num_rows($result)) {
while($post = mysql_fetch_object($result)) {
echo "$post->meta_value";
echo "$post->post_title";
echo "$post->post_content";
}
}
?>
many thanks guys.. .
another problem it show only 1 post and does not latest post
here is my updated code
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("wp_db",$con);
$sql = "SELECT
posts.ID,
posts.post_title AS title,
posts.post_content AS content,
files.meta_value AS filepath
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
AND files.meta_key = '_wp_attached_file'";
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_object($result))
{
echo $row->title . "<br />";
}
mysql_close($con);
?>
do I get something wrong with my code.. .thank for help thank you so much.. .
Hi #idontknowhow:
Your problem appears to be invalid assumptions regarding WordPress' database schema. WordPress stores it's attachments as a post_type='attachment'. Run this query to see what I mean:
SELECT
wp_posts.ID,
wp_posts.post_type
FROM
wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE 1 = 1
AND wp_postmeta.meta_key = '_wp_attached_file'
Records in wp_posts with a post_type='attachment' will typically have 'post_status='inherit' and 'post_parent' equal to your post's ID field value.
Now I didn't completely follow what you were looking to accomplish. You mentioned the technical problems you were having and but not the actual result you were trying to achieve. Without knowing the latter it's hard for me to verify that I understood your question. So I may have misunderstood what you were looking for, but I think this is what you wanted:
SELECT
posts.ID,
posts.post_title AS title,
posts.post_content AS content,
files.meta_value AS filepath
FROM
wp_posts posts
INNER JOIN wp_posts attachments ON posts.ID = attachments.post_parent
INNER JOIN wp_postmeta files ON attachments.ID = files.post_id
WHERE 1 = 1
AND files.meta_key = '_wp_attached_file'
But using direct SQL is frowned upon in the WordPress world; instead use of the WordPress API is the preferred choice if it provides what you need. There are many reasons for this; i.e. to avoid breakage if future WordPress versions change the database schema and because WordPress does a lot of data caching so it might be more performant in everyday usage to not use direct SQL and to use the WordPress API instead.
However, it is not 100% straightforward to use the WordPress API to address your question, though it can be done. If you'd like to see how may I suggest asking over at StackOverflow's sister site WordPress Answers where lots of WordPress enthusiasts can help?
Hope this helps.
-Mike
P.S. I find that a lot of questions about querying the WordPress database here on StackOverflow have people attempt to answer them even though they lack knowledge of the WordPress database schema and more importantly, are not aware of the WordPress API. People here know MySQL really well but often don't know WordPress well enough to give the right answer on WordPress-related questions. WordPress Answers is probably a better place to ask questions about WordPress.
Your query looks correct.
Are you sure you have published posts with _wp_attached_file?
Do the outputs of your queries above have any common records?
Did you tryed to filter mete.meta_key on WHERE? Something like that:
SELECT post.ID, post.post_title, post.post_excerpt,
post.post_content, meta.meta_value
FROM wp_posts AS post
INNER JOIN wp_postmeta AS meta ON meta.post_id = post.id
WHERE meta.meta_key = '_wp_attached_file'
AND post.post_type = 'post'
AND post.post_status = 'publish'
ORDER BY post.post_date DESC
LIMIT 5;

Resources