-
AuthorPosts
-
November 8, 2017 at 11:41 pm #874345
I want to show the posts with a certain tag first (“case”, it’s the only tag used if there is any), then by date (within the posts with the tag “case” and afterwards all others, each newest first).
I added this to my functions.php but I don’t manage to get the date ordering too
add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2); function rr_sort_by_tag($query, $params) { if(is_page(array(724, 3661, 3662))) { $query['orderby'] = "tag"; $query['order'] = "ASC"; } return $query; }
Help :-) Thanks in advance!
Best regards,
JurgenNovember 11, 2017 at 7:11 pm #875561Hey Jurgen,
You can try the code like this
add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2); function rr_sort_by_tag($query, $params) { if(is_page(array(724, 3661, 3662))) { $query['orderby'] = array( 'tag' => 'asc', '{your date field}' => 'asc' ); } return $query; }
If you need further assistance please let us know.
Best regards,
VictoriaNovember 13, 2017 at 12:23 am #875889Hi,
Thanks for your reply!
I tried the code below, but no success… I tried both “post_date” and “date” but not difference…
add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2); function rr_sort_by_tag($query, $params) { if(is_page(array(724, 3661, 3662))) { $query['orderby'] = array( 'tag' => 'ASC', 'post_date' => 'DESC' ); } return $query; }
Thanks for checking!
Best regards,
JurgenNovember 14, 2017 at 9:00 am #876500Hi,
The “tag” is not a valid orderby parameter. You can use the “meta_value” as the orderby parameter but you need to add a custom field for each posts.
// https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
Best regards,
IsmaelNovember 14, 2017 at 2:59 pm #876601Hi Ismael,
So then I should do something like this: create a custom field for each post (for example “tag”) and then sort on this field with some code like this? How about the sort per tag, can this be newest first?
add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2); function rr_sort_by_tag($query, $params) { if(is_page(array(724, 3661, 3662))) { $query['orderby'] = "meta_value"; $query['metakey'] = "tag"; $query['order'] = "DESC"; } return $query; }
Thanks!
November 16, 2017 at 4:53 am #877355Hi,
Please try this filter.
add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2); function rr_sort_by_tag($query, $params) { if( is_page( array( 724, 3661, 3662 ) ) ) { $include = array(); $first = array(); $tagged_args = array( 'meta_key' => 'tag_field', 'meta_compare' => '<=' ); $tagged = get_posts( $tagged_args ); foreach($tagged as $tag) { $first[] = $tag->ID; } $args = array( 'taxonomy' => $params['taxonomy'], ); $posts = get_posts( $args ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($first, $include); // convert values of the $include from string to int function sti($n) { settype($n, 'int'); return $n ; } $include = array_map("sti", $include); $query['post__in'] = $include; $query['posts_per_page'] = 12; $query['orderby'] = 'post__in'; // sort items based on the post__in value } return $query; }
We query the posts with the custom field “tag_field” and then prepend them to rest of the posts.
Best regards,
IsmaelNovember 17, 2017 at 1:02 pm #878041Hi Ismael,
Thank you for the code!
Unfortunately I only see 6 or 7 posts while there are 30 posts. The posts with a custom field “tag_field” (I mentioned this for 2 of my posts) do appear first but I need them all to appear… When I switch to the 2nd and 3rd language I only see the posts with the custom field, and none of the others (while in English I do see a few)…
I assume we are almost there… Thanks again!
Best regards,
JurgenNovember 18, 2017 at 8:36 am #878407Hi,
Please provide a link to the actual page where you’re testing the filter. And provide the WP and FTP details in the private field. We would like to test the modification.
Best regards,
IsmaelNovember 30, 2017 at 2:09 pm #883425Hi Ismael,
Please check the information in the private part to investigate this.
Best regards,
JurgenDecember 1, 2017 at 8:42 am #883766Hi,
Thank you for the info.
I was testing the filter and the site goes down. I’m not sure why. I just modified the functions.php file.
What are the possible values in the “tag_field” custom field aside from “case”?
Best regards,
IsmaelDecember 1, 2017 at 8:48 am #883767Hi,
UPDATE: I found the issue with the code. The site is up again.
The filter is also working as it should. Please let us know if that’s how you want it.
Best regards,
IsmaelDecember 1, 2017 at 11:52 am #883841Hi Ismael,
Thank you so much for helping me out on this!! I just added modified the order so the tagged posts are as well sorted by newest first.
For others reading this topic, this is the final code :-)
function rr_sort_by_tag($query, $params) { if( is_page( array( 724, 3661, 3662 ) ) ) { $include = array(); $first = array(); $tagged_args = array( 'meta_key' => 'tag_field', // 'orderby' => 'meta_value', // 'order' => 'ASC', 'orderby' => array( 'meta_value' => 'ASC', 'post_date' => 'DESC', ), 'meta_query' => array( array( 'key' => 'tag_field', 'value' => array( 'case', 'another' ), 'compare' => 'IN', ), ), 'posts_per_page' => -1 ); $tagged = get_posts( $tagged_args ); foreach($tagged as $tag) { $first[] = $tag->ID; } $new = array( 'post__not_in' => $first ); $new = array_merge($query, $new); $posts = get_posts( $new ); foreach($posts as $post) { $include[] = $post->ID; } $include = array_merge($first, $include); // convert values of the $include from string to int function sti($n) { settype($n, 'int'); return $n ; } $include = array_map("sti", $include); $query['post__in'] = $include; $query['orderby'] = 'post__in'; // sort items based on the post__in value } return $query; } add_filter('avia_masonry_entries_query', 'rr_sort_by_tag', 10, 2);
December 3, 2017 at 2:09 am #884223Hi,
Thank you for sharing your solution, shall we will close this now?Best regards,
MikeDecember 4, 2017 at 11:22 am #884586Hi Mike,
Yes you can do that :)
Best regards,
JurgenDecember 4, 2017 at 10:47 pm #884834Hi,
Glad to hear that. Thanks for using Enfold :)
Best regards,
Nikko -
AuthorPosts
- The topic ‘Custom sort of posts’ is closed to new replies.