Well I am stuck in a sorting posts in wordpress! I know this is a previously asked question, but no luck for me the codes and solutions I found here!
The below is code snippet I am using.
$args = array(
'showposts'=>10,
's' => $search,
'meta_key' => 'neighboorhood',
'meta_value' => $location,
'orderby' => 'meta_value_num',
'meta_key' => 'is_sort',
);
That can not work, since you use meta_key twice in your array. You should use an meta_query for the where clause, and meta_key only for the sort.
$args = array(
'showposts'=>10,
's' => $search,
'meta_query' => array(
array(
'key' => 'neighboorhood',
'value' => $location
)
),
'meta_key' => 'is_sort',
'orderby' => 'meta_value_num'
);
Just use below mentioned code to fetch posts order by meta_value :
<?php
$myargs = array(
'posts_per_page' => 4, //number of post to show
's' => $search,
'meta_key' => 'is_sort', //name of meta field
'orderby' => 'meta_value_num',
'order' => 'ASC', // you can modify it as per your use
'meta_query' => array(
array(
'key' => 'neighboorhood',
'value' => $location,
)
)
);
query_posts($myargs );
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title();?></h2>
<p><?php the_content();?></p>
<?php endwhile;
endif;
wp_reset_query(); ?>
Just use the above mentioned code, you will get your desired result. If you are using this process in case of date field, then please check here for better understanding : http://www.wptricks24.com/how-to-order-wordpress-post-by-custom-field-date
Related
I'm having an issue with WP query filtered by ACF Post Object Field.
I have to query the 'post' filtered by 'author' acf field.
i'm using this code but this don't work
$post_type_query = new WP_Query(
array (
'post_type' => 'post',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova'
)
)
)
);
Thereis one article on wordpress post with 'prova' author, but the query return empty.
I can't understand why
Thanks
Try this:
$postData = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 3,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova',
'compare' => '=' // or if you want like then use 'compare' => 'LIKE'
)
)
)
);
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "Post Title";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
I'm trying to order a wp_query that includes a meta_query...
After some different approaches, it seems that the query can't be ordered since I include the meta_query, and orderby => ID is used by default (I think) and it can't be overwritten. Any ideas on how to accomplish this?
Here's the PHP code I use just to display the posts by meta value:
$paged = get_query_var('paged') ? get_query_var('paged') : (get_query_var('page') ? get_query_var('page') : 1);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'subtitrare',
'value' => 'romana',
'compare' => 'LIKE'
)
)
);
$listing_query = null;
$listing_query = new WP_Query($args);
if ($listing_query->have_posts()) : get_template_part('loop-item');
endif;
wp_reset_postdata();
As you can see I also paginate the results, so i can use the same $args. I've tried to use a custom function to rearrange the results but it didn't worked for me.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'orderby' => 'modified',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'subtitrare',
'value' => 'romana',
'compare' => 'LIKE'
)
)
);
This is the code you need but you already said that you tried this. This will return all the posts which have the custom field value set to LIKE 'romana'. What are the results you get from using this code?
I have a custom post type which has an ACF check box to define if an post is featured or not. I wanted to show 6 featured and 6 non featured posts on a page and so created 2 WP_Query loops both with separate args. Whilst the separation of featured and non-featured is working, the page only shows 6 posts in total and I'm not sure how to resolve that?
My code:
<?php
$args1 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new1 = new WP_Query($args1);?>
<?php while ($new1->have_posts()) : $new1->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( true == $field ):?>
Featured Posts
<?php endif;?>
<?php endwhile; wp_reset_postdata();?>
<?php
$args2 = array(
post_type => 'fairs',
posts_per_page => -1,
showposts => 6 ,
meta_key => 'start',
orderby => 'meta_value_num',
order => 'ASC'
);
$new2 = new WP_Query($args2);?>
<?php while ($new2->have_posts()) : $new2->the_post();?>
<?php $field = get_field( 'wa_feature', get_the_ID() ); if ( false == $field ):?>
Non-featured Posts
<?php endif;?>
<?php endwhile;?>
Your WP_Query isn't right. You do two completely identical queries ($args1 = $args2), and expects diffrent results from them. Also you put meta_key into query arguments, but without telling what and how to compare.
Depending on what type you have chosen, the right syntax, if your meta filed is named featured and the query can be:
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => true //or 1, or 'yes` depending of ACF type you have choose
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => false//or 0, or 'no`
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
or if this featured field exists only in featured posts then
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'EXISTS',
),
)
);
args1 = array(
'post_type' => 'fairs',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC'.
'meta_query' => array(
array(
'key' => 'featured',
'compare' => 'NOT EXISTS',
),
)
);
Check ACF query or Codex for the right sintax.
Your arguments should be quoted:
array(
'post_type' => 'fairs',
'posts_per_page' => 6,
'meta_key' => 'start',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
Array keys can either be integers, or strings. Read more in the PHP array() docs.
You should also only use posts_per_page in this case (and remove showposts, which was replaced by posts_per_page in WP 2.1).
Question for all of ya. I have the current query running on my search page template, and it seems to be working fine if my search query seems to be included in the title of the post, but when I include a meta query to try and also look in another spot for the search term, it doesn't gather any results, only the same results it had before.
Second question, for some reason it still is only displaying 6 (the number of posts set in WP Admin) posts, and not listening to the query.
<?php // WP_User_Query arguments
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => '*'.$search_term.'*',
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$wp_course_query = new WP_Query($args);
// Get the results
$courses = $wp_course_query; ?>
<?php // Check for results
if (!empty($courses->get_posts())) { ?>
<ul class="course-list">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li> <?php the_title(); ?> </li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
<?php } else { ?>
<p>No courses match that query</p>
<?php } ?>
Things I've tried:
Hard coding the value, nothing there.
Removing * from 's'
It seems that this is just impossible in WordPress, so I had to do this another way.
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => $search_term
);
$args2 = array (
'post_type' => 'courses',
'posts_per_page' => -1,
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$courses1 = get_posts($args);
$courses2 = get_posts($args2);
$merged = array_merge($courses1, $courses2);
$post_ids = array();
foreach ($merged as $item) {
$post_ids[] = $item->ID;
}
$unique = array_unique($post_ids);
$posts = get_posts(array(
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $unique,
'posts_per_page' => -1
)); ?>
The following argument works properly for search results, however they aren't displaying in the proper ascending order. I'm looking to display the most expensive at the top, and so on.
Here's my code, where the $input_price is user defined by preset values within a form.
$args = array(
'post_type' => 'post',
's' => $query,
'post_status' => 'publish',
'cat' => "$category_name",
'meta_query' => array(
array(
'key' => 'price',
'value' => "$input_price",
'type' => 'numeric',
'compare' => '<=',
'order' => 'ASC'
)
)
);
$the_query = new WP_Query( $args );
?>
I don't think this is the problem, but the 'price' is being output with currency and comma values, as seen below.
<?php $meta = get_post_custom($post->ID);
echo '$'.number_format($meta['price'][0], 0, '.', ',').''; ?>
Annnd here's the loop to display
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php $meta = get_post_custom($post->ID);
echo '$'.number_format($meta['price'][0], 0, '.', ',').''; ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no products within those search terms! Try Adjusting the maximum price.</p>
Thanks!
try DESC instead of ASC for greatest to smallest?
Found the answer!
The code below works as intended. I re-ordered my array slightly to include 'orderby' at the very end, while also specifying which key to order by.
<?php
$args = array(
'post_type' => 'post',
's' => $query,
'post_status' => 'publish',
'cat' => "$category_name",
'meta_query' => array(
array(
'key' => 'price',
'value' => "$input_price",
'type' => 'numeric',
'orderby' => 'meta_value_num',
'compare' => '<=',
)
),
"meta_key"=>"price",
"orderby"=>"meta_value_num",
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>