Hi I'm trying to make a query to get posts from a specific categories like this:
$args = array('category__in' => array(8,3,12,7));
$posts = new WP_Query($args);
But I need the posts to be displayed in that specific order ( cat id 8 first, then 3, etc ), I can't get it to work properly, posts are displayed according to ASC or DESC names.
Any Help?
You can sort by post__in to use the order of the input values, but with category__in it is a many to many relationship, so using it order by is considerably more difficult and is not supported as far as I know. Also note that WP_Query() does not return an array of posts.
If you have your specific set of ordering rules you can take the results from get_posts() using the category argument, and then use a custom sorting function to order the results using usort() and get_categories().
// function used by usort() to sort your array of posts
function sort_posts_by_categories( $a, $b ){
// $a and $b are post object elements from your array of posts
$a_cats = get_categories( $a->ID );
$b_cats = get_categories( $b->ID );
// determine how you want to compare these two arrays of categories...
// perhaps if the categories are the same you want to follow it by title, etc
// return -1 if you want $a before $b
// return 1 if you want $b before $a
// return 0 if they are equal
}
// get an array of post objects in the 'category' IDs provided
$posts = get_posts( array( 'category' => '8,3,12,7' ) );
// sort $posts using your custom function which compares the categories.
usort( $posts, 'sort_posts_by_categories' );
To my knowledge the best approach here is to make 4 separate queries.
Related
I have following structure
Services
-Page_1
-Page_2
--Page_3
--Page_4
I use Wp-query to get pages. This is my args:
<?php $args = array(
'post_type' => 'page',
'post_parent' => '45'
); ?>
45 is ID of Services page.
But I only get first level Page_1 and Page_2. How do I get all pages? I'm using Advanced Custom Fields plugin so using get_pages() is not a good option, is it?
As mentioned in your comments, the easiest solutions is to actually use get_pages(). You can still grab meta data of posts if you are able to get the the ID of the page (which you can with get_pages()) so you should still be able to ACF's custom fields.
Here's an example of get_pages():
$args = array(
'child_of' => 45,
'post_type' => 'page',
);
$pages = get_pages($args);
The main difference between get_pages() and WP_Query that we want to focus on here is the 'child-of'=>45 paramater vs the 'post_parent'=>45.
The child-of argument will grab ALL children throughout the hierarchy, i.e. children and children's children etc.
In contrast, the post-parent argument of WP_Query will only grab direct children of the page.
Using in conjunction with ACF
If you need to grab custom fields from ACF, you will still be able to use get_post_meta().
If your custom field includes an array of values, you will need to unserialize it first and then loop through the values like this:
$meta = get_post_meta( $post->ID, 'acf_meta_key', true ); //grab the post meta using WordPress core function
$array = unserialize( $meta ); //unserialize the field into an array
foreach ( $array as $value ) { //loop through the array
echo $value .'<br>';
}
I've been stuck for hours in trying to order get_pages() object from wordpress.
I need to order the pages by menu_order (get_pages->menu_order);
I've been trying several PHP functions but none worked in such purpose.
PHP
foreach(get_pages(pll_current_language()) as $page) {
// Somewhere here each page should go up/down in order to be DESC/ASC
$template = get_post_meta( $page->ID, '_wp_page_template', true );
include_once($template);
}
My question is, How can I order this object by one of its values,
example:
I want to order get_pages() by get_pages()->menu_order
Any help will be appreciated.
just use the $args options array and set the sort_order, sort_column on whatever you need, check the docs https://codex.wordpress.org/Function_Reference/get_pages
$args = [
'sort_order' => 'asc',
'sort_column' => 'menu_order'
];
$pages = get_pages($args);
You probably want to use the usort function. You provide it with a comparison function (in your case a function that compares $a->menu_order to $b->menu_order) and it will then use it to sort the elements in your array.
I am wondering if there is a way to list posts from 2 categories in wordpress such that every fifth post is from different category. This is what I mean
Category B
Category A
Category A
Category A
Category A
Category B
and so on....
The sorting for each category will be based on date (descending)
Well its much complicated then its sounds, you wont find an exact method to do this, but you can work around it. For instance in your query you can get posts from 2 categories and then in the loop you can count them and if count is less than 5 display category A, else display category B. But the problem with this is that you dont know how many posts is it going to fetch from each category. It might fetch 2 posts from Category A and 3 From category B. So Instead its better if you use 2 different loops and save all the data in an array.
<?php
$args1 = array( 'posts_per_page' => -1, 'category' => 1 );
$args2 = array( 'posts_per_page' => -1, 'category' => 2 );
$posts_of_category_A = get_posts( $args1 );
$posts_of_category_B = get_posts( $args2 );
$check = 0;
foreach($posts_of_category_B as $posts_B){
// This will only show 1 post of category B;
// echo $posts_B->title (var_dump it)
for($i = $check; $i<$check+4; $i++ ){
// This will Show only first 4 posts of Categroy A
// Convert object to array, something $posts_B[$check]->title
}
$check += 3;
}
Its just a concept, there might be several changes to be made to this script. I know its kind of a hacky way to do this, but i cant think of a better way.
Is there any possible way to sort a new Wordpress post query by the title, but numerically instead of alphabetically?
I have some titles that have a lot of the same name alphabetically, then have a number afterwards, so of course for example Wordpress is putting title12 ahead of title1.
$args = array(
'orderby'=> 'title',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
I know we have this functionality to sort titles in ascending order, but it does not sort titles like that:
Title 1
Title 2
Please let me know if we any work around using WP query ?
Thanks for your help in advance :)
Try adding this immediately after your code above:
usort($loop->posts, function($a,$b) {
return strnatcmp($a->title, $b->post_title);
});
how to get authors details and author page permalink randomly. I have checked the new get_users() function for wordpress which is returning the author but i can not sorting them randomly.
here is the site i am using the code: http://citystir.com
Any help?
Solution: Thanks to theomega i have solved the isse. Here is the code just for community sharing:
$args = array('role' => 'author');
$authors = get_users($args);
shuffle($authors);
$i = 0;
foreach ($authors as $author):
if($i == 4) break;
//do stuff
$i++;
endforeach;
Did not set the limit on the $args because i need the shuffle in all users. Hope it will help someone out there, in the wild. :D Thanks!
Try
$users = get_users('ciriteria');
shuffle($users)
//users is now shuffled
Using the PHP Shuffle-Function.
We can use the get_users() to get a list of authors, users with a specific role, user with specific meta, etc. The function returns users which can be ordered by ID, login, nicename, email, url, registered, display_name, post_count, or meta_value. But there is no random option such as what get_posts() function provides to shows posts randomly.
Since the get_users() function uses WP_User_Query class, there is an action hook pre_user_query we can use to modify the class variable. The idea is using our own ‘rand’ order by parameter. If we put “rand” to the orderby parameter, “user_login” will be used instead. In this case, we need to replace it with RAND() to result users randomly. In this example below, we I ‘rand’ and you can use your own order by name.
add_action( 'pre_user_query', 'my_random_user_query' );
function my_random_user_query( $class ) {
if( 'rand' == $class->query_vars['orderby'] )
$class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );
return $class;
}
The WP_User_Query contains order by query and the our arguments. Now, we have a new order by parameter to WordPress.
$users = get_users( array(
'orderby' => 'rand',
'number' => 5
));
print_r( $users );
Reference: http://www.codecheese.com/2014/05/order-wordpress-users-random/