I have theme that use function to get latest posts showed on homepage. But for some reason , it showing previous post , instead latest one. This is function code frorm theme:
$number_of_news = 3;
if( !empty( $theme_options['home_news_count'] ) ) {
$number_of_news = intval( $theme_options['home_news_count'] );
}
$args = array(
'post_type' => 'post',
'posts_per_page' => $number_of_news,
'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'orderby' => 'post_date',
'field' => 'slug',
'terms' => array('post-format-quote', 'post-format-link', 'post-format-audio'),
'operator' => 'NOT IN'
)
),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
array(
'key' => 'MEDICAL_META_embed_code',
'compare' => 'EXISTS'
),
array(
'key' => 'MEDICAL_META_gallery',
'compare' => 'EXISTS'
)
)
);
// The Query
$blog_query = new WP_Query($args);
// The Loop
if ($blog_query->have_posts()) {
?>
<div class="row">
<?php
while ($blog_query->have_posts()) {
$blog_query->the_post();
global $post;
$format = get_post_format($post->ID);
if (false === $format) {
$format = 'standard';
}
?>
i searched on google to find how to get latest posted blog post and found this but now sure what to modify on my code to show me last one. Any help?
Related
I'm trying to list an archive of queried custom post types named 'jobs', each with a custom field of 'minimum_salary' and 'maximum_salary'.
On the previous search page the user enters two values in the search ('min-salary' & 'max-salary'), which are added to the URL of the archive page.
I want to be able to display ALL jobs that have a 'minimum_salary' AND 'maximum_salary' BETWEEN the min and max salary var the user entered on the previous page.
See below for my code so far, thanks for the help.
<?php
$minSalaryVar = get_query_var('min-salary');
$maxSalaryVar = get_query_var('max-salary');
$taxArgs = array();
if ($minSalaryVar && $maxSalaryVar) {
$taxArgs[] = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'minimum_salary',
'value' => array($minSalaryVar, $maxSalaryVar),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
array(
'key' => 'maximum_salary',
'value' => array($minSalaryVar, $maxSalaryVar),
'compare' => 'BETWEEN',
'type' => 'numeric'
),
)
);
}
if ($taxArgs) {
$args = array (
'tax_query' => $taxArgs,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
You are using 'compare' = 'BETWEEN' two times when you need either using it just once or compare twice one greater than minimum salary and the other bigger. Following I'll let both options:
1:
if ($minSalaryVar && $maxSalaryVar) {
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'minimum_salary',
'value' => $minSalaryVar,
'compare' => '>=',
'type' => 'numeric'
),
array(
'key' => 'maximum_salary',
'value' => $maxSalaryVar,
'compare' => '<=',
'type' => 'numeric'
),
);
}
if ($taxArgs) {
$args = array (
'meta_query' => $meta_query,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
2:
$taxArgs = array();
if ($minSalaryVar && $maxSalaryVar) {
$meta_query = array(
'key' => 'minimum_salary',
'value' => array($minSalaryVar,$maxSalaryVar),
'compare' => 'BETWEEN
'type' => 'numeric'
);
}
if ($taxArgs) {
$args = array (
'meta_query' => $meta_query,
);
$argsNew = array_merge( $wp_query->query_vars, $args );
query_posts( $argsNew );
} ?>
--- EDIT
You should use 'meta_query' instead of 'tax_query'
I am trying to create a short-code to get products from specific category in woocommerce. I am using 'tax_query' to target specific category and 'shortcode_atts' to get parameter from shortcode itself. The code is as follow:
function quick_launch_products($atts) {
extract(shortcode_atts(array(
'product_category_ID' => '',
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_category_ID,
'operator' => 'IN'
)
)
);
echo $product_category_ID;
$products = null;
$products = new WP_Query($args);
}
add_shortcode("quick_launch_product_slider", "quick_launch_products");
The shortcode:
[quick_launch_product_slider product_category_ID="383"]
The return value is blank. I saw a lot of demo codes and followed exactly as they were, but its not working at all.
What am i missing here?
Thanks in advance.
I m trying to implement a multiple meta key filter in my wordpress. That's simple I get value to a form to filter my post. If I implement this with only "price" query wordked perfectly. If I add "genre" nothing work, query not working.
For field "genre" I m using checkbox from Advanced Custom Fields with this structure "homme : Homme / femme : Femme".
I test different thing like delete "price" and query on "genre" not working too...
I get value from this
<?php
if($_GET['minprice'] && !empty($_GET['minprice']))
{
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if($_GET['maxprice'] && !empty($_GET['maxprice']))
{
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 1000;
}
if($_GET['genre'] && !empty($_GET['genre']))
{
$genre = $_GET['genre'];
}
?>
my query looks like this
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
My loop with my query
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
// create a new instance of WP_Query
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
get_template_part( 'content-category', get_post_format() );
?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="clearfix"></div>
<?php bootstrap_pagination();?>
<?php } ?>
<?php else: ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
</div>
<?php wp_reset_query(); ?>
I tested this and it's work !
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
)
);
But that don't work
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
Please, can you help me beacause I m loosing my mind....
Thanks !
I think you are missing an wrapping array in the meta_query
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array( $minprice, $maxprice ),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
),
),
),
);
I am looking for a code that can contain all queries in one.
The part of the code is :
<?php $query = new WP_Query(array ( 'post_type' => 'post', 'posts_per_page' => '6', 'order' => 'DESC', 'tax_query' => array(
array(
'taxonomy' => 'type-article',
'field' => 'slug',
'terms' => array( 'interview', 'tribune' )
) ) ));
while ( $query->have_posts() ) :
$query->the_post(); ?>
<?php if($post->post_type == "post"){ $version_FR = get_field('versionFRexiste'); $langue = get_field('langue'); }; ?>
<?php if($langue == "FR" || ($langue == "EN" && $version_FR == "Non")) : ?>
[some code]
<?php endif; ?>
<?php endwhile; ?>
inside the WHILE, you find two lines with IF
I would like to include these condition in the query at the TOP where WP_Query.
This code is working but my problem is that I would like to have the same number of results to display (6 here)
Thanks for your help
Please have a look here on the ACF Docs.
The answer is to write a meta query within the WP-Query, such as:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'orange'),
'compare' => 'IN',
),
array(
'key' => 'featured',
'value' => '1',
'compare' => '=',
),
),
));
Thanks. It was not so easy... but the metaquery won
<?php $query = new WP_Query(array ( 'post_type' => 'post', 'posts_per_page' => '5', 'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'langue',
'value' => 'FR'
),
array(
'relation' => 'AND',
array(
'key' => 'langue',
'value' => 'EN'
),
array(
'key' => 'versionFRexiste',
'value' => 'Non'
)
)
),
'tax_query' => array(
array(
'taxonomy' => 'type-article',
'field' => 'slug',
'terms' => array( 'interview', 'tribune' )
) ) ));
Hope this will help those who need an answer
I have a search form where my form action is this:
<form action="<?php echo esc_url(home_url('/searched-result')); ?>" method="post">
On the searched-result template I have this query,
<?php
// Only check these form fields (change the list as needed...)
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
if( $_REQUEST[$field] != '' ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
'compare' => 'LIKE'
);
}
}
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 1000,
'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => $meta_query,
);
$query = new WP_Query( $args );
if (have_posts()) : while (have_posts()) : the_post();
?>
<h1 class="title">
<?php the_title(); ?></h1>
The way you have it will only return results if all meta_values match the criteria. You must add an extra argument called relation, and set it as 'OR': Example:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
Not tested :
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
if( isset($_REQUEST[$field]) && !empty($_REQUEST[$field]) ) {
// We have something to match, otherwise ignore the field...
$meta_query[] = array(
'key' => $field,
'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
'compare' => 'LIKE'
);
}
}
Check if your getting value in $_REQUEST.
Hope this helps...
$args = array(
'post_type' => 'property_post',
'posts_per_page' => 1000,
'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
$meta_query
)
);