I have this code,
$type = get_the_ID();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$units3 = new wp_query( array( 'post_type' => 'units' ,
'posts_per_page'=> 6 ,
'paged' => $paged ,
'meta_key' => 'unittype',
'meta_value' => $type ) );
while ($units3->have_posts()) : $units3->the_post();
Its must get me all post from unit That is in the unit type that id = $type
But the query is get me all post.
It must get me only all post from unit post type that in unit type post type,
Where is error
Meta key is for using meta_value_num in order parameter,
You need to use meta_query for querying specific meta data
Test your query first until you get the correct data
$data = query_posts( array(
'post_type' => 'your_custom_post_type',
'orderby' => 'meta_value_num',
'meta_key' => 'your_order_meta_key',
'posts_per_page'=> 5,
'meta_query' => array(
array(
'key' => 'key_to_only_display_if_exist',
'value' => array( 3, 4 ),
'compare' => 'IN',
)
)
)
);
var_dump($data);
Then Use query_post
query_posts( array(
'post_type' => 'your_custom_post_type',
'orderby' => 'meta_value_num',
'meta_key' => 'your_order_meta_key',
'posts_per_page'=> 5,
'meta_query' => array(
array(
'key' => 'key_to_only_display_if_exist',
'value' => array( 3, 4 ),
'compare' => 'IN',
)
)
)
);
if (have_posts()) :
while (have_posts()) : the_post();
echo '<div class="post-entry">';
echo '<h2>' . get_the_title() . '</h2>';
echo '<div class="entry-content">'. apply_filters('the_content',get_the_content('Read More')).'</div>';
echo '</div>';
endwhile;
endif;
wp_reset_query();
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 having a problem getting my query function. I need to run the loop, excluding a particular category.
I'm trying to use category__not_in, but is not working at all some.
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( '44' ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
query_posts($query);
?>
I've already tried:
'category__not_in' => array( '44' ),
'category__not_in' => array( 44 ),
'category__not_in' => '44',
'category__not_in' => 44,
But nothing works =(
Try using tax_query instead :
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => get_query_var('paged'),
'tax_query' => array(
array(
'taxonomy' => '<YOUR TAXONOMY NAME>',
'field' => 'term_id',
'terms' => array( 44 ),
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
query_posts($query);
?>
Use 'cat' => '-44' in your $args array:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => '-44',
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
It's the way recommended in the WP Codex.
Thanks guys, it worked thanks to #rnevius
The problem was in my query, I was using WP_Query() and query_posts().
I used how reference the WP Codex: https://codex.wordpress.org/Class_Reference/WP_Query
Below is how my code was at the end:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category__not_in' => array( 44 ),
'posts_per_page' => 9,
'paged' => get_query_var('paged')
);
$query = new WP_Query( $args );
?>
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
// code
<?php
}
} else {
// no posts found
}
wp_reset_postdata();
?>
To exclude a category in the search use this:
function search_filter($query)
{
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search)
{
$taxquery = array(
array(
'taxonomy' => 'category',
'field' => 'term_taxonomy_id',
'terms' => 244,
'operator' => 'NOT IN',
)
);
$query->set( 'tax_query', $taxquery );
}
}
}
add_action('pre_get_posts','search_filter');
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 );
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 ?>
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 );
?>