get latests posts from my wordpress on my webpage - php

I am trying to get the latest posts on my blog. These are being displayed in a little widget in my front page on my website. I am getting a little trouble though. The posts are getting multiplied. http://goo.gl/Q2STSC, Here is my SQL query.
$posts = DB::connection('blog')->select("
SELECT a.post_title AS title, a.post_name AS slug, meta_value AS thumbnail,a.post_content AS contenido, a.post_date AS fecha
FROM wp_postmeta, wp_posts AS a
JOIN wp_posts AS b ON a.ID = b.post_parent
WHERE b.ID = wp_postmeta.post_id
AND meta_key = '_wp_attached_file' AND a.post_status = 'publish'
ORDER BY fecha
LIMIT 0 , 6");

Try this
<?php $args = array(
'posts_per_page' => 6,
'offset' => 0,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
foreach ( $posts_arrayas $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;?>
Please use above code to fetch latest post from you blog.In above code you can see different parameters
posts_per_page : Number of post to get
offset : Starting Post number likeyou do in custom query limit 0[offset],6[posts_per_page]
order : Orderby [ DESC / ASC]
orderby : date [ column on which order clause has to perform]
post_type : post ( type of post] ( if you want to get posts for custom posts like news, then you can pass news as post_type
post_status : publish ( status of post )
Also in foreach you can permalink [ link of the post ] and the_title( title of the post)
For more details you can check here https://codex.wordpress.org/Function_Reference/get_posts

Related

Can't order wordpress posts

I have there the code I'm using, and I'm trying to get the posts in the order of the IDs from the $menus array but don't do that, he gave me the posts from the newest to oldest...I have tried to use order with DESC but the array did not change.
$menus = array(105, 54, 111);
$args = array(
'post__in' => $menus,
'orderby' => 'ID',
'order' => 'DESC',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
$i = 1;
while ( $query->have_posts() ) : $query->the_post();
do_action('fwp_before_post_content');
get_template_part('extend-helpers/' . $layout);
do_action('fwp_after_post_content');
$i++;
endwhile;
else:
get_template_part('extend-helpers/content', 'none');
endif;
UPDATE:
The posts are from different categories.
UPDATE II:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (105,54,111) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.menu_order, FIELD( wp_posts.ID, 105,54,111 ) LIMIT 0, 1000"
Why I have ORDER BY wp_posts.menu_order? because in $args I don't have orderby menu_order..
You need to add the "orderby" item in the args you pass WP_Query.
You can find the documentation here
Have you seen the WP_query loop? https://code.tutsplus.com/tutorials/mastering-wp_query-using-the-loop--cms-23031. It uses $args=ASC and DSC and 'orderby ' =>'id' too.
for the first part, you want to use post__in also in orderby line as Juan suggested in his comment. From the documentation: 'post__in' - Preserve post ID order given in the 'post__in' array. So, your code should look like this
$menus = array(55, 53, 57); // I have used mine ids
$args = array(
'post__in' => $menus,
'orderby' => 'post__in',
);
$query = new WP_Query( $args );
For the second part, which is more important, why do you have ORDER BY wp_posts.menu_order. You can find answer also in comments. The query is being altered by something (theme or plugin), check pre_get_posts hook, try switching theme, etc.
I have tested this code and it is working, so it is ordering posts as they are in array $menus.

How do I retrieve data as a Wp_query object with this custom mysql 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;
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.

WordPress fetching all posts of a custom post type

When I'm fetching all posts for a custom post type with the get_posts() function I always get a memory size limit has been reached even if their are only 300 posts I want to get from the database.
Increasing the memory is not an option and enabling pagination neither.
What can cause this and how can I solve this. Because from each of these posts I will later also have to fetch the meta data and split it up in 2 arrays/objects and a lot more stuff. It needs to be able to handle 5000+ posts/records per page request.
Any tips, ideas, suggestions, something else?
First try:
$args = array(
'posts_per_page' => 300,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'partners',
'post_status' => 'publish',
'suppress_filters' => true,
/*'meta_key' => 'partner-submission-status',
'meta_value' => 'goedgekeurd',*/
);
$posts = get_posts($args);
var_dump(count($posts));
Second try:
$query = $wpdb->get_results("
SELECT p.ID, p.post_title
FROM wp_posts as p
INNER JOIN wp_postmeta as m
on p.ID = m.post_id
WHERE p.post_type = 'partners' AND
p.post_status = 'publish'
");
var_dump(count($query));die;
But both of them don't even get to the var_dump
<?php query_posts( 'post_type=your_post_type&posts_per_page=10' );
if (have_posts()) : while (have_posts()) : the_post(); ?>
...........
<?php endwhile; endif; wp_reset_query(); ?>

PHP: How to display posts of specific category using a custom query in WordPress?

I'm not using WP language but simple PHP
my code now is
SELECT * FROM wp_posts w
WHERE w.post_status = 'publish'
AND w.post_type = 'post'
ORDER BY RAND()
LIMIT 3
so i can display 3 random blog article on my custom homepage (not WP)
it works.
but now i'd like to choose a specific category
how can i do it?
query_posts('cat=123');
This does the trick for a specific category. If the category has a name, equal the value to the name. Although, category ID is recommended (if later changes happen).
You really shouldn't be using a SQL query but rather WP_Query object or query_posts function.
Your query should look like this:
$posts = new WP_Query(array(
'orderby' => rand,
'posts_per_page' => 3,
'post_status' => 'publish',
'cat' => 123
));
If you are using this on your home page then the "query_posts" function would fit better:
query_posts(array(
'orderby' => rand,
'posts_per_page' => 3,
'post_status' => 'publish',
'cat' => 123
));
Category ID is "123" in this case but you can use any of the category params here instead.
global $wpdb;
$posts = $wpdb->get_results('SELECT ID, post_title AS title, post_excerpt AS excerpt FROM '.$wpdb->posts.' WHERE post_type = "post" AND post_status = "publish" ORDER BY post_date DESC LIMIT 5');

Wordpress order posts by custom meta in pagination

I'm ordering my posts by a custom meta value named "size".
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'size'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->postmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts):
global $post;
foreach ($pageposts as $post):
setup_postdata($post);
the_title();
endforeach;
endif;
wp_pagenavi(); //creates page navigation
At the same time I'm using the WP-pagenavi plugin to navigate the posts by pages. I have 10 posts on each page.
The problem: Posts are ordered separately in each page. How can I order posts in descending order through all pages?
Update: I might have found a solution but I'm not sure how to implement it in my code
http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
$my_query = new WP_Query( array( 'tag' => 'foo', 'paged' => get_query_var('paged') ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
I've found a SO post that can solve your problem :
How to sort a 'query_posts' function by custom field, while limiting posts by another custom field
There you will find a custom class extending WP_Query and allowing you to make a query ordered by a custom field, and to include the paged query var too.
So the steps :
Past the class PostsOrderedByMetaQuery code somewhere like in your functions.php
Replace your query by :
// Retrieve `paged` in URL
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Make the query like in WP_Query but with our custom class
$query = new PostsOrderedByMetaQuery(array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'orderby_meta_key' => 'size',
'orderby_order' => 'DESC'
));
Use it !
while ( $query->have_posts() ) : $query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $query ) );
wp_reset_postdata(); // avoid errors further down the page

Categories