order by custom field - wordpress - 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..

Related

Wp Meta query does not work while meta value does

I have two queries which I believed should both work because I believe they are exactly the same
The one is as follows ( Works perfectly, and gives me the posts I need )
$args2 = array(
'post_type' => 'vacancy',
'meta_key' => 'full_or_part_time',
'meta_value' => 'part_time',
)
The other one is as follow : ( Returns all posts, ignores the meta query )
$args2 = array(
'post_type' => 'vacancy',
'meta_query' => array(
array(
'key' => 'full_or_part_time',
'value' => "part_time",
)
),
);
I cannot figure out why option two does not work. Any assistance will be greatly appreciated.

Wordpress get_posts not working as expected

I'm trying to retrieve posts in Wordpress using the get_posts function, trying to filter by a custom field named cegep_region but the method is returning posts with any value in this field. My query below:
$cegep = get_posts(array(
'post_type' => 'cegep',
'orderby' => 'rand',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'cegep_region',
'value' => '386',
'compare' => '='
)
));
When I look into the database, 386 is not what there is in the meta_value.
select * from wp_postmeta where post_id=577 and meta_key='cegep_region'
What can I possibly be doing wrong?
Try without using compare and add one more array inside in meta_query
$args = array(
post_type' => 'cegep',
'meta_query' => array(
array(
'key' => 'cegep_region',
'value' => 386,
)
)
);
$postslist = get_posts( $args );
its work fine with me

Sort posts by custom field number

I'm using this query do get some posts.
$args = array(
'post_type' => 'spaces',
'post_per_page' => '500',
'orderby' => 'rand',
'meta_key' => 'space-city',
'meta_value' => $search,
);
$query = new WP_query($args);
Now I need to order the results by total of comments on each post. I've a custom field with this number called "space-comments", but I've no idea how to sort this posts with this second meta_key.
I made some tests, but I only was able to get post when "space-comments" has a value. When there is no value, the post don't show up.
Any ideia how can I start?
The WP_Query can accept a meta_query argument that is populated as an array of sub-arguments. That array can have sub arrays that are each their own meta query, so you can create some nice compound searches across meta data.
See the example from https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters below.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );

Use 'LIKE %word%' for ACF Query Wordpress

I use ACF plugin on my wordpress site and I want to code custom research.
I have many products and I want to display products which contain the searched word.
I do my query like this :
$args = array(
'post_type' => 'product',
'meta_key' => 'brand',
'meta_value' => $word,
'compare' => 'LIKE');
$the_query = new WP_Query($args);
But this display only products with the brand which exactly matches with $word.
for exemple if I search "yan" I want to display products with the brand "YANMAR", "POLYAN", "TRYANPO", etc.
How to do this please ?
Thank you and have a good day !
Try below code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'brand',
'value' => $word,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args);

Wordpress output links to previous and next posts from custom query

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.

Resources