I created a function that retrieves all parent posts of a post and i'm ordering by post_date.
select * from {$wpdb->posts} where post_type = 'page' and post_status = 'publish' and post_parent = " . ($parent == 0 ? $page_id : $parent) . ' order by post_date asc
I the WP backend the posts are ordered differently and it's not by publish date nor by ID.
Any idea how i can order my query so that i will have the same order as in the backend?
Thanks,
Radu
Go through this article here are six ways to customize the sort order of posts in WordPress.
6 Ways to Customize WordPress Post Order
Also must see this link :
http://wpquestions.com/question/show/id/1009
Related
I am trying to run a SQL query through my functions.php file and once that works, move it into a plugin and that way, by activating it the query will run. I can then run the query by activating / deactivating the plugin.
The goal is to change ALL products with the status published into drafts. Here's what I got, which does not work.
Anyone here who can help?
add_action('wp_head','turn_all_products_into_drafts');
function turn_all_products_into_drafts(){
include_once("wp-config.php");
include_once("wp-includes/wp-db.php");
global $wpdb;
$sql = "UPDATE wp_posts SET post_status = 'draft' WHERE post_type = 'product' AND post_status = 'publish'";
$wpdb->update($sql);
}
Could it be that you have spelling error ?
In description you say :
with the status published
and then in select you have :
post_status = 'publish'
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
I am having an issue that i couldn't find on google and elsewhere.
To put problem in the context.
I am making a wordpress site where user can put their tutor listing for giving a tutorials, and i am implementing the system of evaluation of the tutors them self and their listening.
So I have tutor_listing post type and each tutor_listing has a 'post_score' meta value, and each tutor (user who puts his tutorials) has user_meta value called 'user_points'.
So I need a WP_Query that will get from the database all published tutor_listing but sorted by (post_score + user_points). Just to emphasis 'post_score' is a post_meta of the post_type tutor_listing and 'user_points' is user_meta of author of that post (tutor_listing).
Also i need that value to show in the loop.
I can get all the tutor_listing by
$args = array(
'post_type' => 'job_listing',
'post_status' => 'publish'
);
$query = new WP_Query( $args );
I can get the result from the custom wordpress sql query like this:
SELECT p1.ID, p1.post_author, p1.post_title, p1.post_date, (um1.meta_value + pm1.meta_value) AS total_score
FROM $wpdb->posts p1
JOIN $wpdb->users u1 ON (p1.post_author = u1.ID)
JOIN $wpdb->usermeta um1 ON (um1.user_id = u1.ID)
JOIN $wpdb->postmeta pm1 ON (pm1.post_id = p1.ID)
WHERE p1.post_type = 'tutor_listing'
AND p1.post_status = 'publish'
AND um1.meta_key = 'user_points'
AND pm1.meta_key = 'post_score'";
Can WP_Query provide such a functionality?
I need WP_Query to use its all extra functionality like pagination ect.
Or is there a way i could all the result done with the my custom sql give to WP_Query object?
Thank you
I use the following SQL query to fetch custom posts in Wordpress.
I ended up with this solution because i needed to fetch und sort the posts based on custom meta data. This woks pretty well so far!
But how do i incorporate custom taxonomies in my query?
Say for example only fetch posts that are associated with term_id 5?
I'm really stuck here cause i can't figure out how wp_terms, relations etc are connected...
Any help is greatly appreciated!
UPDATE:
After some reading the solution in my case is this:
WordPress stores the relations between taxonomies and posts in wp_term_relationships (where object_id is the post_id and term_taxonomy_id ist the term_id in wp_terms). So if i want to only fetch posts that belong to a specific term_id, i came up with the following query. Seems to work as far as i can tell!
This is my query
global $wpdb;
$activelang = ICL_LANGUAGE_CODE;
$cmonth = date('Y-m');
$myquery = "
SELECT wposts.*, wpostmeta.meta_value AS date
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_icl_translations wicl_translations, , wp_term_relationships wptermrelations
WHERE wposts.post_status = 'publish'
AND wpostmeta.post_id = wposts.ID
AND (wptermrelations.object_id = wposts.ID AND (wptermrelations.term_taxonomy_id = '29' OR wptermrelations.term_taxonomy_id = '30'))
AND wposts.post_type = 'event'
AND wicl_translations.element_id = wposts.ID
AND wicl_translations.language_code = '$activelang'
AND (wpostmeta.meta_key = '_ws_prem_date' OR wpostmeta.meta_key = '_ws_date1_date' OR ... ... OR wpostmeta.meta_key = '_ws_date10_date')
AND wpostmeta.meta_value >= '$cmonth-01'
ORDER BY CAST(date AS DATETIME),wposts.post_title ASC
";
$myloop = $wpdb->get_results($myquery);
In case someone is wondering what this query does:
This query fetches custom post types (events that have multiple dates associated) and builds a loop for these events that allows for duplicate entries since a 'normal' WordPress loop always filters out any duplicate entries.
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";