Wordpress ordering posts by meta key - wordpress

This has been confusing me no end.
I have this query on a page, works fine to order posts by meta_value.
query_posts( array( 'meta_key' => 'epicredrank', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged, ) );
Trying to create a link to order the posts like this and it isn't working.
<a class="voted-on" href="<?php bloginfo('url'); ?>?meta_key=epicredrank&orderby=meta_value&order=DESC">
<span>Hot Posts</span>
</a>
It doesn't re-order anything, the query, when passed to the URL seems to do nothing at all.
What am I doing wrong?
Edit- Want to sort posts by meta value via a link.

Try this
<a class="voted-on" href="<?php bloginfo('url'); ?>?meta_key=epicredrank&orderby=meta_value_num&order=DESC"><span>Hot Posts</span></a>
$meta_key = mysql_real_escape_string((isset($_GET['meta_key']) ? $_GET['meta_key'] : 'epicredrank' ));
$order_by = mysql_real_escape_string((isset($_GET['orderby']) ? $_GET['orderby'] : 'meta_value_num' ));
$order = mysql_real_escape_string((isset($_GET['order']) ? $_GET['order'] : 'DESC'));
query_posts( array( 'meta_key' => $meta_key, 'orderby' => $order_by, 'order' => $order , 'paged' => $paged ) );

Related

Wordpress loop: custom field value 'yes' > order by taxonomy 'a' and custom field value 'no' > order by taxonomy 'b'

Tried searching and testing for two days now, but don't seem to get this loop working in the way that I want it. Any help would be greatly appreciated.
I want to loop through a custom post type (I know how to do this)
In this loop, I first would like to list a few posts (number can vary) with a custom field value ('yes'), and then sort these posts based on a taxonomy value (length).
Then I would like to list the rest of the posts (these have a custom field value 'no') and sort these posts based on a taxonomy value (year).
These pages use pagination. I tried two different loops but since the first part is an undefined number of posts, this does not work for me. And on top of that it just feels 'cleaner' in one loop if possible.
So far this is what I have:
<?php $args = array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => 24,
'paged' => $paged,
'meta_query' => array(
'first-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'yes',
),
'other-posts' => array(
'meta_key' => 'highlighted-post',
'meta_value' => 'no'
),
),
'orderby' => array(
'first-posts' => 'ASC',
'other-posts' => 'DESC'
),
); ?>
You have to make two queries, one for the 'yes' posts and another for the 'no'. Also, your query is wrong when calling meta query.
We set the pagination:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
The 'yes' posts:
<?php $yes_posts = get_posts(array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => 24,
'paged' => $paged,
'meta_query' => array(
array(
'meta_key' => 'highlighted-post',
'meta_value' => 'yes',
),
),
'order' => 'ASC'
)); ?>
The 'no' posts:
<?php $no_posts = get_posts(array(
'post_type' => 'custom-post-type-name',
'posts_per_page' => 24,
'paged' => $paged,
'meta_query' => array(
array(
'meta_key' => 'highlighted-post',
'meta_value' => 'no'
),
),
'orderby' => 'DESC'
)); ?>
We combine the queries to get a list of post ids for the last query:
$allposts = array_merge($yes_posts, $no_posts);
$postids = array();
foreach( $allposts as $item ) {
$postids[] = $item->ID;
}
We create an array of post IDs
$unique = array_unique($postids);
$args = array(
'post__in' => $unique,
'paged' => $paged,
'orderby' => 'post__in'
);
We do the loop:
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
$checking = 0;
while ( $the_query->have_posts() ) { $the_query->the_post();
// Handle posts here
}
wp_reset_postdata();
}
Combine every code in a page and it should work.

Filtered a query in WordPress

To display some items from a post type, I'm using this WordPress query:
$posts = get_posts(array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC'
));
But how can I filter the datas returned by this query depending the infos in the post type page ? For example, I have a input 'year' to get the year of the project.
Thanks.
You can use wp_query like Below
$args = array (
'post_type' => array( 'realisations' ),
'post_status' => array( 'publish' ),
'order' => 'ASC',
'orderby' => 'date',
'year' => 'yourinputyear' // 2021
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post();
echo get_the_title();
endwhile;
}else{
echo "Data not found";
}
Please try this way. Hope is useful.
Thanks
You can use Date Parameters check below code.
$posts = get_posts( array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC',
'date_query' => array(
array( 'year' => 'yourinputyear' )
)
) );

Only display upcoming events in Wordpress Widget

Through a shortcode I placed the following code which is intented to display a random upcoming event in the sidebar. At the moment it displays a random of ALL events. How would I change the code to only display events that are younger than the current date?
<?php
// Build a custom query to get posts from future dates.
$querystr = `
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'f21_event_startdate'
AND STR_TO_DATE(wpostmeta.meta_value,'j. M Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostmeta.meta_value,'j. M Y') RAND
LIMIT 1
`;
$events = $wpdb->get_results($querystr, OBJECT);
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post); ?>
<?php the_field('f21_event_startdate'); ?>
<h2><a style="color:#1e73be;" href="<?php the_permalink() ?>"><?php the_title(); ?></h2>
<div style="margin-top:10px; margin-bottom: 10px;"><?php the_post_thumbnail('full'); ?></div></a>
<b><?php the_field('f21_event_sub_header'); ?></b>
<?php endforeach;
else : ?>
<li>Sorry, no events coming up.</li>
<?php endif; ?>
you can WP_Query(). check the code below.
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'f21_event_startdate',
'meta_value' => date('Ymd'),
'meta_compare' => '>=',
'orderby' => 'rand',
'order' => 'ASC'
);
$upcoming_events = new WP_Query( $args );
Avoid the SQL query in WordPress for getting post-type data.
we have a function for that please use
$args = array(
'post_type' => 'tribe_events',
'post_status' => 'publish',
'posts_per_page' => 10,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'meta_type' => 'DATETIME',
'paged'=>$page,
'meta_query'=>array(
array(
'key' => '_EventStartDate',
'value' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
'compare' => '>'
),),
'tax_query'=>array(array(
'taxonomy' => 'tribe_events_cat',
'field' => 'id',
'terms' =>$tribe_events_cat,
))
);
$events = new WP_Query($args);
Summary: if you are using custom fields in WordPress make sure your custom fields are stored as a post meta then you can easily filter that,
In the above code, we have a meta query where we can use key, value, and compare. thanks.

When sorting posts by post meta in WP_Query, only posts with a meta value are returned

I need to sort posts by rating. And this code does the job well:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'post_type' => 'brands',
'posts_per_page' => 6,
'meta_key' => 'post_average_rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'paged' => $paged
);
$brands = new WP_Query( $args );
?>
But there is a problem. If a post does not have a rating value, it simply does not appear. As you can see, I need six posts to be displayed on the page. But if all posts have a rating in only three, only three will be displayed. And I would like to display post without a rating also. Please tell me how this can be solved?
thanks FluffyKitten's comment the solution was found. Here is the code:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'post_type' => 'brands',
'posts_per_page' => 6,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'post_average_rating',
'compare' => 'EXISTS'
),
array(
'key'=>'post_average_rating',
'compare' => 'NOT EXISTS'
)
),
'paged' => $paged
);
$brands = new WP_Query( $args );
?>
Posts with a rating are now displayed first, sorted from highest to lowest, and posts that have no rating are just displayed after them.
Reference: Orderby meta value only returns posts that have existing meta key on wordpress.stackexchange.com

WP: how to query posts by variations?

I am trying to do a query my wocommerce products by their variations, so I did:
$args = array(
'meta_key' => 'flower-type', // attribute slug
'meta_value' => 'fresh-roses', // attribute value
'meta_compare' => 'LIKE'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
but unfortunately I always get no results, so what is the issue here?
This type of data is saved in a dynamic created meta key so the attribute name counts when making the query, in your case I assumed the slug for your attribute name is "flower-type", you can check this in your database to confirm.
The meta key that you want to use is compose out of the word attribute and the name of the attribute you created when making the variations fresh-flowers in your case.
Be careful that changing this in the admin will "break" your query.
So the arguments will look like:
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'attribute_flower-type',
'value' => 'fresh-roses',
'compare' => 'LIKE',
),
),
);
please notice the compare attribute used, it might work with = but first confirm that it works with LIKE and then you can play with it.
Try below argument for passing into WP query:
args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'flower-type',
'value' => 'fresh-roses',
'compare' => '=',
),
),
);

Resources