query_posts exclude a meta key - wordpress

<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => featured_product, 'meta_value' => 1)); ?>
<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC')); ?>
I have 2 queries, first to show meta key with featured_product eq 1. So I want to exclude all the featured products on the second query. How can i do that please? Thanks!

You simply need the meta_compare for that:
<?php query_posts(
array(
'showposts' => 1000,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'featured_product',
'meta_compare' => '!=',
'meta_value' => 1
)
); ?>

While looping through your first query, build an array consisting of the ids of the posts in the loop. Then use pass this array to the second query using "post__not_in" => $your_array

Related

I want to query all posts with ACF date field today and beyond

I'm banging my head against the wall with this, is there anything wrong with my code?
I want to query all posts that have 'launch_date' today and beyond.
<?php
// find date time now
$date_now = date('M, d');
// query events
$args = array(
'posts_per_page' => 20,
'post_type' => 'project',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'launch_date',
'compare' => '<=',
'value' => $date_now,
'type' => 'DATETIME'
)
),
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'launch_date',
'meta_type' => 'DATE'
);
echo build_query( $args );
?>

Using WP_Query twice on the same page with different results

I am trying to get recent posts, loop through those and then get the rest of the posts afterward. Here's how my loop is currently structured:
$recentArgs = array(
'posts_per_page' => 4,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$recentQuery = new WP_Query($recentArgs);
$recent_post_ids = [];
foreach ($recentQuery->posts AS $recentPost) {
$recent_post_ids[] = $recentPost->ID;
}
Then I loop through and do the HTML. Afterwards I do this:
<?php wp_reset_query(); wp_reset_postdata(); ?>
I've also tried with no luck:
<?php rewind_post(); ?>
Here is the 2nd WP_Query call, which returns the same result as above:
$allQuery = new WP_Query([
'posts_per_page' => 10,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'exclude' => $recent_post_ids,
]);
I'm sure I'm missing something stupid/simple. But, any help is much appreciated.
There may be a better way to do this, but I worked around it by using get_posts() instead. So, now my first and second (with diff arguments) query looks like this:
<?php wp_reset_query(); wp_reset_postdata(); ?>
<?php
$allPosts = get_posts([
'posts_per_page' => 10,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'exclude' => $recent_post_ids,
]);
Thanks again.

Oder By Date In post Meta

I am using wp_query to get post but i want it's order by date saved in my post meta
I try it using meta_key and order_by but it's not working
$args = array(
'post_type' => 'obituary',
'posts_per_page' => 10,
'paged' => $paged,
'meta_key' => 'user_date_of_death',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
and date saved format in post meta is 06/28/2016
Use below argument in the post query.
$today_date = date('m/d/Y', strtotime('+2 hours'));
$args = array( 'post_type' => 'obituary',
'order' => 'ASC',
'meta_key' => 'user_date_of_death',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'user_date_of_death',
'value' => $today_date ,
'compare' => '>=',
'type' => 'DATE'
)
)
);

WP_Query Search is not working.....for keyword 'TATTOO'

This search is not working.... Any guess?? This is a very simple piece of code.
It is for showing records related with keyword tattoo for title field. It shows all products. The search is not triggered.
$args = array(
'posts_per_page' => $per_page,
'offset' => $paged,
'post_type' => 'product',
'meta_key' => $orderby,
'orderby' => $num_orderby,
'order' => $order,
'search' => '*tattoo*',
'search_columns' => array('post_title')
);
$new_query = new WP_Query( $args );`
http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
try use 's' instead 'search'
...
's' => '*tattoo*',
...

Create a filter in a page template

I am using Realto theme for wordpress I am wondering if I am on the correct path. I need to display properties from a city. So I decide to create a page template to add the location
This are the default arguments but I dont know how to filter the results by city
$args = array(
'numberposts' => '',
'posts_per_page' => $posts_per_page,
'offset' => 0,
'cat' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'property',
'post_mime_type' => '',
'post_parent' => '',
'paged' => $paged,
'post_status' => 'publish'
);
I tried adding
'locations' => 'MYCITY'
but it didnt work
This is an example of the search results when I search by city, so I am basing my arguments on this.
/?post_type=property&search_keyword=&locations=MYCITY&property_type=proyectos&beds=&baths=&status=&min-price=&max-price=
I assume that locations is a custom field? Maybe this works:
<?php
$args = array(
'post_type' => 'property',
'meta_key' => 'locations',
'meta_value' => 'MYCITY'
);
?>
Visit http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for more details.

Resources