Wordpress output links to previous and next posts from custom query - wordpress

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!

Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

Related

Get users list by published post count in last month ago

I want get lists of users by published posts count in a month ago.
I have this code but dosnt work, just show registered users in a month ago
<?php
$args = array(
'orderby' => 'post_count',
'order' => 'DESC',
'role' => 'Subscriber',
'number' => '4',
'date_query' => array(
array(
'after' => '12 hours ago',
'inclusive' => true,
),
),
);
$user_query = new WP_User_Query( $args );
Try this code
Change query array to this
$post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_count',
'order' => 'DESC',
'posts_per_page' => 4,
'date_query' => array(
array(
'after' => '1 month ago'
)
)
);
$post_query = new WP_Query($post_args);
then in a loop use the_author() function to show your users lists by published post count
if ($post_query->have_posts()) {
while ($post_query->have_posts()) {
$post_query->the_post();
the_author();
}
}
I think your problem is that you are trying to do two types of query in one go.
Remember that this query is for users - the date_query is applied to user query and not posts.
You need to retrieve authors with posts first and then based on their ID you can retrieve their posts with post query.
I think this should look something like this (not tested and please correct me if wrong):
$args = array(
'orderby' => 'post_count',
'order' => 'DESC',
'role' => 'Subscriber'
);
$user_query = new WP_User_Query( $args );
$authors = $user_query->get_results();
if (!empty($authors)) {
foreach ($authors as $author){
$author_info = get_userdata($author->ID); //user data
$author_id = $author->ID;
$args = array(
'author' => $author_id,
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 10,
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
$the_query = new WP_Query($args);
//post loop here
}
}
The above code should get all Subscribers and retrieve their ID's, for each user there is another query based on their ID to retrieve their posts.
Source 1: https://wordpress.stackexchange.com/questions/109710/get-posts-get-all-posts-by-author-id
Source 2: https://developer.wordpress.org/reference/classes/wp_user_query/
Source 3: https://wordpress.stackexchange.com/questions/99265/display-posts-of-the-last-7-days

Multiple custom field sorting on wordpress archive page

I have been through several topics on sorting taxonomies and custom fields, including some that were promising. However, I am still not constructing my query args correctly it seems.
I am "fixing" someone else's code in a custom theme that is used in a multisite configuration, but I am able to override/filter the query on a per-site basis.
The query is called via the theme with the following:
return apply_filters( 'theme_query_args_filter', $args);
I have custom fields in the "role" taxonomy that include:
last_name
first_name
Pretty common I think.
However, when the args are executed, the following filter args are sorting only by last name (key part is the else clause):
function my_args_filter( $args ) {
if (is_home() && !is_search()) {
$args['meta_key'] = 'event_date';
$args['orderby'] = 'event_date';
$args['order'] = 'DESC';
}
else {
$tax = $args['taxonomy'];
$theterm = $args['term'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
}
add_filter( 'theme_query_args_filter', 'my_args_filter' );
I've tried to modify the orderby as indicated in https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ to use an array to do a multisort, but I'm hitting my head up against the wall. I think the problem is that the code is written using a mixture of old ways of doing things and new ways.
Any advice is appreciated. According to the example on in the docs above, I SHOULD be able to pass in multiple meta key/value/orders via an array, but I'm just not getting it.
Thanks for any leads you might have. (long-time listener, first-time caller)
(I also looked at https://wordpress.stackexchange.com/questions/109849/order-by-desc-asc-in-custom-wp-query but I couldn't extrapolate that example to this one either)
Figured it out on my most recent try. Hat tip to this solution https://wordpress.stackexchange.com/questions/249881/multiple-custom-fields-for-orderby-in-wp-query by Nath. if there is a more elegant way to do it, please let me know.
$tax = $args['taxonomy'];
$theterm = $args['term'];
$paged = $args['paged'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'paged' => $paged,
'posts_per_page' => '10',
'meta_query' => array(
array(
'relation' => 'AND',
'last_name' =>
array(
'key' => 'last_name',
'compare' => 'EXISTS',
),
'first_name' =>
array(
'key' => 'first_name',
'compare' => 'EXISTS',
),
),
),
'orderby' => array (
'last_name' => 'ASC',
'first_name' => 'ASC',
)
);
}

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' => '=',
),
),
);

WordPress meta_query for Custom Post Type with orderby

I'm trying to sort out a WordPress query for a Custom Post Type, but I can't make it work.
The post type is events. An Advanced Custom Fields field called sticky (yes/no) is used for sorting (stickies on top), and another ACF field called glamrock (yes/no) is used to exclude certain posts.
The result is, glamrock gets excluded, but stickies are not sorted.
If I delete the meta_query, sorting works fine, but glamrock posts are included.
$bpb_args = array(
'numberposts' => -1,
'post_type' => 'events',
'posts_per_page' => 100,
'paged' => get_query_var( 'paged', true ),
'meta-key' => 'sticky',
'meta_query' => array(
array(
'key' => 'glamrock',
'value' => 'no',
'compare' => 'IN',
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
);
$bpb_query = new WP_Query( $bpb_args );
if( $bpb_query->have_posts() ):
while( $bpb_query->have_posts() ) : $bpb_query->the_post();
//show post
endwhile;
endif;
Update:
Unfortunately, meta_value instead of meta_value_num didn't change anything. It still seems to be sorting by date/time.
The Advanced Custom Field type is Radio Buttons.
In addition to the arguments, I also included the loop.
You need to specify only meta_value since your meta-key is non numeric
'orderby' => 'meta_value'
#Mihai had it right: meta_value_num was the main issue. I changed it to meta_value.
Here's the query/loop that worked:
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'sticky',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'lizenz_erteilt',
'value' => 'no',
'compare' => 'LIKE',
),
)
);
$query = new WP_Query( $args );
// Loop
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
//show post
endwhile;
endif;

order by custom field - wordpress

I'm trying to sort a page of posts by a custom field.
Here's what I have so far, I'm just not sure how or where to add the orderby
$args = array(
'post_type' => 'new',
'meta_query' => array(
array(
'key' => 'over-make',
'value' => 'Doral',
'compare' => 'LIKE'
)
)
);
$loop = new WP_Query( $args);
You would use orderby on the same level as post_type and meta_query in your example.
$args = array(
'orderby' => 'meta_value',
'post_type' => 'new',
'meta_query' => array(
array(
'key' => 'over-make',
'value' => 'Doral',
'compare' => 'LIKE'
)
)
);
$loop = new WP_Query( $args);
(WordPress Codex: WP_Query)
It is probably most suitable to use the get_posts() function:
get_posts('orderby=meta_value_num&meta_key=keyname');
Sources: Get Posts and Interacting with WP Query and Order By Parameters
ps. love the idea of ordering by a meta value, hadn't thought of it before, but it could make several different sorting system easier to build, including a popularity mechanism..

Resources