How To Properly "Order By" This Arg - php

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 );
?>

Related

WP: Simplify a glossary php script/code

I would like to simplify my code to avoid any repetition of the same code format. I did a custom post type of Glossary from A-Z, though i did a basic custom Query Post but it end-up to messy code output.
<?php
$args = array (
'post_type' => array( 'cre-library' ),
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array( array( 'key' => 'post_category', 'value' => 'A', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
);
$query = new WP_Query ( $args );
if ($query->have_posts()) :
echo '<div id="content-for-a" class="active">';
echo '<h2>A</h2>';
while ($query->have_posts()) : $query->the_post();
the_title('<h3>','</h3>');
the_content();
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
?>
<?php
$args = array (
'post_type' => array( 'cre-library' ),
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array( array( 'key' => 'post_category', 'value' => 'B', 'compare' => 'LIKE', 'type' => 'CHAR' ) )
);
$query = new WP_Query ( $args );
if ($query->have_posts()) :
echo '<div id="content-for-b">';
echo '<h2>B</h2>';
while ($query->have_posts()) : $query->the_post();
the_title('<h3>','</h3>');
the_content();
endwhile;
echo '</div>';
endif;
wp_reset_postdata();
?>
<?php //...and so on ?>
This code is working already. if we can simplify this into one loop block of PHP with having it matched post_category value per entry and display it all the post at the same time.

Wordpress Search Query - Meta Queries & Custom Fields

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
)); ?>

Meta_query in WooCommerce

I'm trying to use the meta_query in WooCommerce product page.
This is the code I'm using:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' =>8,
'meta_query' => array(
array(
'key' => 'autor',
'value' => '"'.get_the_ID().'"',
'compare' => 'LIKE',
)
),
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_field('autor');
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
It doesn't show any title, if I remove the meta_query it shows all products so the problem is that the relation meta_query code is not working. Any ideas how to use it on WooCommerce template?
You use get_the_ID() to get the author id in meta_query args.
get_the_ID() - will get the Post id, not the author id.
To get all posts by authoк id your args should look like this:
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
I also see that you using get_field()-function. WordPress core does not have this function. You can use instead get_the_author().
Eventually your code will look like:
<?php
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_the_author();
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
You can use this code snippet for meta_query in woocommerce
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page'=> 10,
'orderby' => 'total_sales',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_featured',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'total_sales',
'value' => '10',
'compare' => '>='
)
)
);
$query = new WP_Query( $args );

How to fetch posts order by meta value?

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

WordPress Advanced Custom Fields - Meta Query no result

I am using WPML and ACF in my WP.
Now I wanna list posts from the category ID 399 with the ACF Field "organization_type" and the value key "socialbusiness" but they do not show up.
This are my query tries:
$args = array(
'post_type' => 'post',
'cat' => 399,
'posts_per_page' => -1,
'meta_query' => array(
//'relation' => 'OR',
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
//'compare' => '='
)
)
);
//unset($args);
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'cat' => 399,
'meta_key' => 'organization_type',
'meta_value' => 'socialbusiness'
);
// query
query_posts( $args );
while( have_posts() ) {
What am I doing wrong?
You should have just one variable $args because your first declaration of the variable is override by your second variable.
In your case your code should look like :
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 399
)
),
'meta_query' => array(
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
'compare' => '=',
'type' => 'CHAR'
),
)
);
$items = new WP_Query($args);
?>
<?php if($items->have_posts()) : ?>
<div class='item'>
<?php while($items->have_posts()) : $items->the_post() ?>
.....
<?php endwhile ?>
</div>
<?php endif ?>

Categories