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 );
Related
some of my products missing their images, what I'm trying to do is to make a query that will give me only products with the default image (woocommerce place holder image).
this is what i have tried :
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query', array(
array(
'key' => '_thumbnail_id',
'value' => '5',
'compare' => '=='
)
)
);
i found place holder image id using this function :
attachment_url_to_postid("/wp-content/uploads/woocommerce-placeholder.png");
the query returns every single product I have, and not only those with the placeholder image, what causes it, and is there a better way?
You can check using meta_key _thumbnail_id. Try the below query.
global $wpdb;
$post_ids = $wpdb->get_results( "
SELECT ID FROM $wpdb->posts
WHERE ID NOT IN (
SELECT post_id from $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
)
AND post_type = 'product'
AND post_status = 'publish'
" );
echo "<pre>"; print_r( $post_ids ); echo "</pre>";
I am trying to display last couple of post title in a comma separated value with below code.Here I am making the query from wp_post table.Now How can I get the category in this code as there is no column for category in wp_posttable. below is my code
<?php
$posts = $wpdb->get_col("
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE post_status = 'publish'
and post_type='post'
ORDER BY post_date DESC LIMIT 10");
$the_posts = array();
foreach($posts as $post) :
echo implode( ', ', $the_posts );
?>
You can do it this way:
1 - get posts (correct way for wp)
<?php
$args = array(
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
2 - loop the posts and get categories
foreach($posts_array as $post):setup_postdata($post);
$category = get_the_category();
var_dump($category);
endforeach;
wp_reset_postdata();
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";
SELECT
IF (POSITION('Villa' IN p.post_title )>=1,
SUBSTRING(p.post_title, 7), post_title) AS 'Title',
p.post_title,
p.ID,
p.post_content
FROM wp_posts p INNER JOIN wp_term_relationships tr
ON p.ID=tr.object_id where tr.term_taxonomy_id=4
and p.post_status='publish'
ORDER BY Title ASC;
I can run above query to get data with "wpdb" function like
$wpdb->get_results($query);
But I need the above results to be returned as a Wp_query object as I want to use functions like get_the_excerpt()
The orderby parameter accepts the post__in sorting value as a possible way to sort posts returned. With orderby set to post__in, the posts will be returned in the order they are entered into the post__in parameter.
The following code
args = [
'post__in' =>[3, 1, 2],
'orderby' => 'post__in'
];
$q = new WP_Query( $args );
will return posts 1,2, and 3 in the following order
3,1,2
Try this code,
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status'=>'publish',
'orderby'=>'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => 4
)
)
);
$query = new WP_Query($args);
$query_posts = $query->posts;
var_dump($query_posts);
You can take your WPDB results and use the post functions like this...
global $wpdb;
global $post;
$query = "SELECT
IF (POSITION('Villa' IN p.post_title )>=1,
SUBSTRING(p.post_title, 7), post_title) AS 'Title',
p.post_title,
p.ID,
p.post_content
FROM wp_posts p INNER JOIN wp_term_relationships tr
ON p.ID=tr.object_id where tr.term_taxonomy_id=4
and p.post_status='publish'
ORDER BY Title ASC;"
$result = $wpdb->get_results( $query );
foreach( $result as $post )
{
setup_postdata( $post );
echo "<div>" . get_the_excerpt() . "</div>";
}
Make sure you set the $post global.
I filtered the post's in my sidebar by using this code,
$mostlikequerystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->postmeta.meta_value DESC
LIMIT 0 , 10";
This code working perfect , but now i want to add a category filter too..
for this i used $term_id
global $wpdb;
$term_id = get_term_by('slug','trailers');
$term_id->term_id;
echo $term_id;//Prints 12
$mostlikequerystr="SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish')
LIMIT 0 , 10";
$tariler_post = $wpdb->get_results($mostlikequerystr, 'OBJECT');
echo $wpdb->show_errors();
but its not working for me and no error too ...
You are missing with conditional operator in your WHERE clause
WHERE ($wpdb->term_taxonomy.term_id = $term_id here
^^^^^
$wpdb->posts.ID = $wpdb->postmeta.post_id
Try this one by adding AND
$mostlikequerystr = "
SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->postmeta.meta_value DESC
)
LIMIT 0 , 10";
If you are using WPDB class try to catch the errors
<?php $wpdb->show_errors(); ?>
Other way you can use WP's built in functions
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'most_liked',
'value' => '',
'compare' => '!='
),
'category__in' => 'id here or skip this argument'
'orderby' => 'meta_value_num',
);
$tariler_post=new WP_Query($args);