Using WP_Query twice on the same page with different results - wordpress

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.

Related

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*',
...

Wordpress 'pre_get_posts' not working on ajax?

Im using a plugin to select featured post which uses 'pre_get_posts' to filter posts.It works fine in normal query_post() but it is not working inside ajax. Here is my code
<?php
add_action('wp_ajax_portscroll', 'portscroll');
add_action('wp_ajax_nopriv_portscroll', 'portscroll');
function portscroll(){
?>
<?php
$offset_click= $_POST['data'];
$offset= 4+$offset_click*2;
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 2,
'featured' => 'yes',
'orderby' => 'menu_order',
'order' => 'ASC',
'offset'=> $offset
);
$post_osrtfolios = query_posts($args);
if ($post_osrtfolios) :
foreach ($post_osrtfolios as $post_osrtfolio) :
///contents goes here
endforeach;
endif;
wp_reset_query();
} ?>
Everything works fine, query works fine but the parameter 'featured' => 'yes' is not working. This parameter is from plugin.
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => '_is_featured',
'value' => 'yes'
)
),
'orderby' => 'menu_order',
'order' => 'ASC',
'offset'=> $offset
);
Use this

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.

Wordpress Query on multiple post types

I am completely stumped on this. The code below allows me to query multiple post types. I break them down like this because of the use of categories. The weird thing is, I only get posts from the post_type = 'post'. The final query I use post__in to establish the posts that I want by ID. If I print out $post_ids, I get the exact IDs that I am looking for. But my final query doesn't give me those IDs. Thoughts??
$postArgs = array(
'post_type' => 'post',
'cat' => '16,17,18',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$videoArgs = array(
'post_type' => 'occ-videos',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$photoArgs = array(
'post_type' => 'occ-photography',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$docArgs = array(
'post_type' => 'wpfb_filepage',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$posts_query = get_posts($postArgs);
$docs_query = get_posts($docArgs);
$video_query = get_posts($videoArgs);
$photo_query = get_posts($photoArgs);
// start putting the contents in the new object
$all_posts = array_merge($posts_query, $docs_query, $video_query, $photo_query);
$post_ids = wp_list_pluck( $all_posts, 'ID' );//Just get IDs from post objects
print_r($post_ids);
$artArgs = array(
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'post__in',
'post__in' => $post_ids);
$artQuery = get_posts($artArgs);
My understanding is that Wordpress always defaults to the post_type of post. So it's only finding posts that have one of those IDs – and ignoring your custom post types.
Trying adding a line to to your $artArgs
$artArgs = array(
'post_type' => array('post','page','occ-videos','occ-photography'), //Add this line
'posts_per_page' => 20,
'post_status' => 'publish',
'orderby' => 'post__in',
'post__in' => $post_ids
);
And add whatever post types you need Wordpress to query.

query_posts exclude a meta key

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

Resources