I am trying to delete WordPress post after X Days.
And i am using this code.
$daystogo = 30;
$sql =
"UPDATE {$wpdb->posts}
SET post_status = 'trash'
WHERE (post_type = 'post' AND post_status = 'publish')
AND DATEDIFF(NOW(), post_date) > %d";
$wpdb->query($wpdb->prepare( $sql, $daystogo ));
But i want to exclude some post on meta key.
Like i don't want to delete that post which have FEATURD POST VALUE IS 1
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
)
),
Any way to add this condition in query??
Thanks
Here is mysql code for you, you can use MySQL "IN()" function for this:
$sql =
"UPDATE {$wpdb->posts}
SET post_status = 'trash'
WHERE (post_type = 'post' AND post_status = 'publish')
AND ID not in (select post_id from {$wpdb->postmeta} where
meta_key='featured_post' and meta_value='1' )
AND DATEDIFF(NOW(), post_date) > %d";
Related
add_action('transition_post_status', 'send_new_post', 9876543210, 3);
function send_new_post($new_status, $old_status, $post) {
if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'product') {
global $wpdb;
$current_product_id = $post->ID;
if(isset($current_product_id)) {
$post_id = $current_product_id;
} else {
$post_id = '131';
}
$sql = "SELECT
post_id as id,
(SELECT post_title FROM wp_posts WHERE id = pm.post_id) AS title,
(SELECT post_name FROM wp_posts WHERE id = pm.post_id) AS name,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_price' LIMIT 1) AS price,
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_regular_price' LIMIT 1) AS 'regular_price',
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_stock' LIMIT 1) AS stock,
IFNULL((SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_sku' LIMIT 1),
(SELECT meta_value FROM wp_postmeta WHERE post_id = pm.post_id AND meta_key = '_custom_field' LIMIT 1)) as sku
FROM `wp_postmeta` AS pm
JOIN wp_posts AS p ON p.ID = pm.post_id
WHERE p.ID = ".$post_id." AND meta_key in ('_product_version')
AND p.post_status in ('publish')";
$results = $wpdb->get_results($sql);
$result = json_decode(json_encode($results), true);
$data_init = $result[0];
$tablename=$wpdb->prefix.'product_init';
$data=array(
'post_id' => $data_init['id'],
'post_title' => $data_init['title'],
'post_name' => $data_init['name'],
'price' => $data_init['price'],
'regular_price' => $data_init['regular_price'],
'sku' => $data_init['sku'],
'stock' => $data_init['stock'],
'created_by' => 'Created By Custom Code'
);
$wpdb->insert( $tablename, $data);
}
}
Somehow i only get this
I can understand by i am not getting other values price, regular_price, sku, and stock ..........
I tried to get posts from wordpress sql, I want get posts where post_type = 'post' and where post_type = 'page'
My code:
$wpdb->get_row( " SELECT * FROM $wpdb->posts WHERE post_type = 'post' and post_type = 'page' AND post_status = 'publish' ORDER BY RAND() " );
This code is correct
$wpdb->get_row( " SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY RAND() " );
but only post_type = 'posts' i want both! post and page.
About Wordpress SQL
Thanks.
You can use OR:
$wpdb->get_row( " SELECT * FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND post_status = 'publish' ORDER BY RAND() " );
...but I still don't understand why you don't do a standard WP_Query().
$args = array(
'post_type' => array('post', 'page'),
'orderby' => 'rand',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
I've following query -
$query = array(
'post_type' => 'accessory',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'accessory-bike',
'value' => array("33"),
'compare' => 'IN'
)
)
);
And now I've this record in database -
520 is the ID for accessory post type. It still returns 0 results. I can't figure where the hell did I do wrong ? Stupid wordpress.
SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1
AND wp_posts.post_type = 'accessory'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private')
AND ( ( wp_postmeta.meta_key = 'accessory-bike' AND CAST(wp_postmeta.meta_value AS SIGNED) IN ('33') ) )
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
The value of accessory-bike on post 520 isn't 33; it's a serialized string.
I can see you're using Advanced Custom Fields so all you need to do is pick a different field type that doesn't save data in a serialized string. You may find that something like a custom taxonomy is more appropriate.
I just want to sort posts from a standard query based on last comment date. I tried something like this (code below) but I can't get it right. PS: 'dzialy' => $cat this is custom taxonomy named dzialy and $cat is specified id of that category.
function forum_commentsjoin($join) {
global $wp_query, $wpdb;
if ($wp_query->query_vars['post_type']=='forum' && isset($wp_query->query_vars['dzialy'])) {
$join .= "LEFT JOIN $wpdb->comments ON $wpdb->comments.comment_post_ID=$wpdb->posts.ID";
}
return $join;
}
...and later on...
$args = array( 'post_type' => 'forum', 'dzialy' => $cat, 'posts_per_page' => $ilosc, 'orderby'=>'comment_date', 'order'=>'DESC', );
print_r($loop->query);
...after that…
Array ( [post_type] => forum [dzialy] => 1468 [posts_per_page] => 20
[orderby] => comment_date [order] => DESC )
You can use JOIN and wpdb class to run your raw sql queries
SELECT DISTINCT p.*
FROM
`wp_posts` p
LEFT JOIN `wp_comments` c ON(p.`ID`=c.`comment_post_ID`)
WHERE p.`post_status`='publish'
AND p.`post_type`='forum'
ORDER BY c.`comment_date` DESC
try this code,
select wp_posts.*,
coalesce(
(
select max(comment_date)
from $wpdb->comments wpc
where wpc.comment_post_id = wp_posts.id
),
wp_posts.post_date
) as mcomment_date
from $wpdb->posts wp_posts
where post_type = 'post'
and post_status = 'publish'
order by mcomment_date desc
limit 10
Reference : Ordering Wordpress posts by most recent comment
Hope this helps you
I write my own query to wordpress database and I'm stuck. I have many (5) custom fields like:
town
price
size
... etc
and in search.php I have:
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id ";
$town = addslashes($_GET['town']);
if($town!=''){
$querystr .= " AND wpostmeta.meta_key = 'town' AND wpostmeta.meta_value = '".$town."'";
}
$mo = addslashes($_GET['mo']);
if($mo!='' && preg_match("/^\d+$/", $mo)){
$querystr .= " AND wpostmeta.meta_key = 'price' AND wpostmeta.meta_value > '".$mo."'";
}
$md = addslashes($_GET['md']);
if($md!='' && preg_match("/^\d+$/", $md)){
$querystr .= " AND wpostmeta.meta_key = 'price' AND wpostmeta.meta_value < '".$md."'";
}
$querystr .= " AND wposts.post_status = 'publish' AND wposts.post_type = 'post'";
$pageposts = $wpdb->get_results($querystr, OBJECT);
but this does not work. If I use only one condition:
$querystr = "SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id ";
$town = addslashes($_GET['town']);
if($town!=''){
$querystr .= " AND wpostmeta.meta_key = 'town' AND wpostmeta.meta_value = '".$town."'";
}
$querystr .= " AND wposts.post_status = 'publish' AND wposts.post_type = 'post'";
$pageposts = $wpdb->get_results($querystr, OBJECT);
then it will work. What I'm doing wrong?
The select expression doesn't make sense as a whole because you have contradictive where conditions. It also doesn't make sense in relation to how relational databases work. You want to match two unique rows that share the same column names in one query, this is not possible without using techniques like subqueries.
Consider that all expression parts must be true and you got something like this:
SELECT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'town' AND wpostmeta.meta_value = 'My town'
AND wpostmeta.meta_key = 'price' AND wpostmeta.meta_value > 500
Here you say meta_key equals "town" AND meta_value equals "My town". That makes sense, but when you also say meta_key also equals "price" AND meta_value is also greater than 500. The expression will never be true and regardless the parser also has no way of grouping together the two different condition sets.
WP_Query
If possible I suggest that you use the WP_Query class instead of directly querying the database. This wrapper greatly simplifies your code and makes it easier to maintain. Note that the code requires WordPress >=3.1 as it uses the meta_query option.
Your query can be written like this:
<?php
// The arguments that defines the query
$args = array(
'post_status' => 'publish',
'post_type' => 'post'
);
// We define the meta/custom field conditions
$meta_query = array();
// PS: No need to slash the values, WordPress will do that for us
$town = $_GET['town'];
$mo = (int) $_GET['mo']; // Simple sanitizment, implement your own as see fit
$md = (int) $_GET['md']; // Simple sanitizment, implement your own as see fit
if ( $town ) {
$meta_query[] = array(
'key' => 'town',
'value' => $town
);
}
if ( $mo ) {
$meta_query[] = array(
'key' => 'price',
'value' => $mo,
'type' => 'NUMERIC',
'compare' => '>'
);
}
if ( $md ) {
$meta_query[] = array(
'key' => 'price',
'value' => $md,
'type' => 'NUMERIC',
'compare' => '<'
);
}
if ( $meta_query ) {
// Add the meta_query conditions to the arguments array
$meta_query['relation'] = 'AND';
$args['meta_query'] = $meta_query;
}
// Create WP_Query object with the arguments
$query = new WP_Query( $args );
// Fetch the posts
$posts = $query->get_posts();
// Voila!
?>