I have an old function which uses the post excerpt to hold the image thumbnail. It was a bit hackey and worked for a long time.
Now I need to use the post excerpt for, you know, an excerpt. So I'm looking to update this function to grab the image src info straight from the post attachments instead.
The question:
How can I update the $before_sql SQL code below to grab the first attached image in the post attachment?
The code:
(I think I am only concerned about the sql portion as the rest should clean itself up?) There's more code section too, but instead of pasting ALL of it here, this snippet should be enough.
$before_sql = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' and post_date < '$cur_post_date' ORDER BY post_date DESC LIMIT $thumbnum";
$before_results = $wpdb->get_results($before_sql);
if($before_results) {
foreach ($before_results as $before_result) {
$post_title = stripslashes($before_result->post_title);
$permalink = get_permalink($before_result->ID);
$post_excerpt = ($before_result->post_excerpt);
$output="<div class=\"thumbnails\">" . $post_excerpt . "<br />‹</div>\n " . $output;
}
}
To get the first attachment with the posts in one query you can do in this way
SELECT *,
(SELECT guid FROM `wp_posts` WHERE post_type ='attachment' AND post_parent=wp.`ID` ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM `wp_posts` wp
ORDER BY post_date ASC will get the first image if you want the latest uploaded image you can simply use DESC ORDER BY post_date DESC
Here is your query
$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' ORDER BY wp.post_date DESC LIMIT $thumbnum";
It works fine for me
This is the query which will fetch those posts only which has the attachments on it
$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' HAVING attachment IS NOT NULL ORDER BY wp.post_date DESC LIMIT $thumbnum";
Related
I want to create a search page for my website, but my SQL query doesn't return what I want.
This is my SQL query
SELECT * FROM blog_posts WHERE post_content LIKE '%" . Addslashes($_GET['s']) . "%' AND post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC lIMIT 0, 4;
But it only return the published posts, it doesn't return the searched post
This is my search page : https://lite.the-scientist.fr/search
So, I've recently taken on a job making changes to the WordPress theme for arena . govspace . gov . au and I've been asked to look into why posts posted on the same day do not appear in order according to the time they were posted.
Now, the way the content is pulled in is not the way I am used to doing it in WordPress. I would normally use a WP_Query, so the examples below are a little out of my wheelhouse.
This function is responsible querying for the output for this page: https://arena.govspace.gov.au/news-media/media-releases/
function SQLQueryPost( $post_type,$meta_key,&$paged,&$rows,&$total_row, $limit_query = "limit 10"){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wpdb;
$test = $wpdb->posts;
$sql = 'SELECT SQL_CALC_FOUND_ROWS meta_value, id, post_title
FROM '.$wpdb->posts.' join '.$wpdb->postmeta.' on '.$wpdb->posts.'.id = '.$wpdb->postmeta.'.post_id
WHERE
meta_value != "" and meta_key = "'.$meta_key.'"
and post_status = "publish"
and post_type = "'.$post_type.'"
order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC
'.$limit_query.' offset '.($paged - 1 )*10;
$rows = $wpdb->get_results($sql);
$total_row = $wpdb->get_var( "SELECT found_rows();" );
}
For this query, I simply changed the line order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC to order by post_date DESC and magically it now orders them by date and time.
This function is responsible for pulling more than one content type for this page: https://arena.govspace.gov.au/news-media/
function SQLQueryPostMulti( $post_types,$meta_key,&$paged,&$rows,&$total_row, $limit_query = "limit 10"){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wpdb;
$test = $wpdb->posts;
$sql = 'SELECT SQL_CALC_FOUND_ROWS meta_value, id, post_title, post_type
FROM '.$wpdb->posts.' join '.$wpdb->postmeta.' on '.$wpdb->posts.'.id = '.$wpdb->postmeta.'.post_id
WHERE
meta_value != ""
and post_status = "publish"
and post_type IN (' . $post_types . ')
order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC
'.$limit_query.' offset '.($paged - 1 )*10;
$rows = $wpdb->get_results($sql);
$total_row = $wpdb->get_var( "SELECT found_rows();" );
}
This query seems to be a little trickier. I attempted the same change (order by STR_TO_DATE( meta_value, "%d-%m-%Y") DESC to order by post_date DESC) but in this case the page returns the exact same entry for all 10 results.
Basically, if anyone could provide any kind of insight as to how I can make this query not only order the posts by day, but to also order them by time posted on that day, I would be extremely grateful.
I have been trying to fetch posts from a wordpress site's mysql db to my own script for a few days.
<?php
$querystr = "
SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'tag'
AND $wpdb->postmeta.meta_value = 'email'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
I can see thumbnail images on the original site but I could not figure out where to get that image location data in the db.
What would be the enhancements on my query in order to have image information with current ones?
You need to dive into your wp_postmeta table. This is where you will find everything. You can get the thumbnail ID with the following query:
SELECT * FROM 'wp_postmeta' WHERE post_id=**ID_HERE** AND meta_key='_thumbnail_id'
You will get a thumbnail ID from which you will be able to get your real thumbnail with the following query:
SELECT * FROM 'wp_postmeta' WHERE post_id=**PREVIOUSLY_OBTAINED_ID**
This will actually give you two rows :
One for the URL of your image (meta_key="_wp_attached_file")
One for the image info like its dimensions, its title etc... (meta_key="_wp_attachment_metadata")
Try this code:
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
More details here: http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
I want a select box that allows users to pick the order of posts displayed. (Like youtube order by date / relevance)
I'm displaying a list of custom post types (products). I want users to be able to select the order by price & size. (these are both custom fields).
I've written this code which allows me to change the order by changing the default variables below.
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $wpdb->postmeta.meta_value $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
I now need to let public change the default variables with a select box, but I don't know how.
<form method="post">
<select name="custom_field_choice">
<option value="size">Size</option>
<option value="price">Price</option>
</select>
<input type="submit" value="order_select" />
</form>
Is it possible to allow a user to change a php variable with a select box? If not, whats the best way to do this?
Try to change your code to receive variable from http post request like this
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
$orderField = $post->custom_field_choice;
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $orderField $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
Option's values size and price must be column name on your database table.
I am trying to ORDER BY my results of a custom select query.
But I am trying to order by the value of a meta key.
Please see my query below...
$get_atts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->posts.meta_key = 'surname' ASC");
As you can see this ORDER BY is breaking it...
ORDER BY $wpdb->posts.meta_key = 'surname' ASC"
So I am trying to order by the value of surname
But my does not seem to work. Can any explain why or help?
Try to use this query:
$get_atts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'surname'
AND $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->postmeta.meta_value ASC");