Wordpress - multiple WP Query objects into one? - wordpress

In Wordpress it's possible to create own WP Querys for the loop. An example is this:
$my_query = new WP_Query(array('post_parent' => 3, 'post_type' => 'page'));
Another example is this:
$my_query = new WP_Query(array('cat' => 1, 'post_type' => 'post'));
I want a loop that presents pages AND posts from the same loop.
Now to my question. Is it possible to combine these two objects into one? If it is, how? I'm NOT interested in creating two different loops.

In case you don't want to do it with SQL, this is how I did my search page.
Basic problem: When doing a meta_query, wordpress thinks I want the condition to be joined with "AND" instead of "OR".
So Wordpress looks for a page with title/content = "myContent" AND the aioseop_keyword "myContent". This (in my case) lead to zero results, despite there was a page with matching SEO keyword.
To get around this, I make two queries. Sounds simple, BUT: the Loop didn't want to recognize the posts, despite there are posts in the $post object. I found this solution after taking a look on the have_posts() function : it refers to other variables than just the $post object.
$term = get_search_query(); // same as $_GET['s']
# the normal search:
$wordpress_keyword_search =& new WP_Query(array(
's' => $term,
'showposts' => -1
));
# now push already found post IDs to an array, so we can exclude them from the meta search.
foreach ($wordpress_keyword_search->posts as $post_)
$exclusion[] = $post_->ID;
# now do the meta query search
$aioseop_keyword_search =& new WP_Query(array(
'post__not_in' => $exclusion,
'post_type' => 'any',
'showposts' => -1,
'meta_query' => array(
array(
'key' => '_aioseop_keywords',
'value' => $term,
'compare' => 'LIKE',
)
)
));
# merge the two array posts.
# post_count and found_posts must be added together also.
# otherwise have_posts() returns false.
# see: http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/query.php#L2886
$wordpress_keyword_search->posts = array_merge($wordpress_keyword_search->posts, $aioseop_keyword_search->posts );
$wordpress_keyword_search->found_posts = $wordpress_keyword_search->found_posts + $aioseop_keyword_search->found_posts;
$wordpress_keyword_search->post_count = $wordpress_keyword_search->post_count + $aioseop_keyword_search->post_count;
Then use this in a simple loop:
if ($wordpress_keyword_search->have_posts()) {
while($wordpress_keyword_search->have_posts()) {
$wordpress_keyword_search->the_post();
# now you simply can:
the_title();
the_content();
}
} else {
echo '<p>Sorry, no posts found</p>';
}

what you want would translate to a WHERE ... OR ... condition or a UNION in SQL, eg.
SELECT * FROM posts WHERE (post_parent = 3 AND post_type = 'page')
OR (cat = 1 AND post_type = 'post')
or
SELECT * FROM posts WHERE post_parent = 3 AND post_type = 'page'
UNION
SELECT * FROM posts WHERE cat = 1 AND post_type = 'post'
from looking at the source and the way WP constructs SQL from WP_Query(), i don't think this is possible: there is no OR'ing nor UNION of query vars.
the only thing that comes to my mind is writing a plugin that implements the posts_where filter (applied to the WHERE clause of the query that returns the post array). you could call this plugin with your different WP Querys, and the plugin would get their WHERE parts and could OR them together.
see also http://codex.wordpress.org/Custom_Queries

Related

WP Query Get Posts by Comment Author ID and specific keyword?

I am trying to create a custom loop which gets the posts that author__in 1 has commented the specific keyword 'test' on and use that to build the recent posts list.
<?php $query_args = array('search' => 'test', 'author__in' => '1', 'post_status' => 'publish', ); $the_query = new WP_Comment_Query( $query_args );?> <?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
This query doesn't work, it gives me nothing, i guess ->have_posts does not exist for wp_comment_query?
How can i build a query using WP_query that lists the most recent posts with comment 'test' from author with id 1? Is it even possible?
Couple things here.
First, the "search" parameter is actually only "s", not the full word. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Second, the author__in param takes an array, like this: $query = new WP_Query( array( 'author__in' => array( 2, 6 ) ) ); rather than a single integer or quoted digit. Here's that reference: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
I think you could get what you want by just changing your code slightly. One thing to note, I'm not sure if the search parameter does a keyword search in comments though, it might only search post content. Try this:
$query_args = array('s' => 'test', 'author__in' => array(1), 'post_status' => 'publish', );
Let me know if this works for you.

Wordpress show all Custom Post Type except one

I am using this loop:
<?php
$search_count = 0;
$search = new WP_Query("s=$s & showposts=-1");
if($search->have_posts()) : while($search->have_posts()) : $search->the_post();
$search_count++;
endwhile; endif;
echo $search_count;
?>
How can I say, that I want to show every Custom Post Type, except for the post type called 'klanten' (clients in Dutch)?
You can use like this:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => array( 'post', 'page', 'movie', 'book') // include the posts type you want to show
);
$query = new WP_Query( $args );
Unfortunately, there's currently no way you could exclude a specific post_typeby defining it. So, the workaround is you only define the post_type you want to include.
A couple of years too late, but you can use the get_post_types() function with the operator parameter set to not. This way, you will receive an array of all post types except the one(s) you set in the args parameter of the function, which you can then use to define the post_type in your query:
$args = array(
's' => $s,
'posts_per_page' => -1,
'post_type' => get_post_types(array('name' => 'klanten', 'public' => false), 'names', 'not')
);
$query = new WP_Query( $args );
However, you might want to var_dump() the result of the function first, since it will return other hidden post types you might not be interested in (like 'attachments' or others defined by plugins). This is the reason why I added 'public' => false, which will make sure that I only receive public post types. This removes most of the undesired post types, but you should still check ('attachments' is not removed by this).
Another shortcoming of this method is that you can only remove one post type. A workaround for that would be to get all post types and then remove the ones you don't want from the array using the method described here, but that seems like too much code, so in that case you might be better off just setting all the post types you want instead of the ones you don't.

Getting list of all custom post type posts and regular posts from a certain category

I'm trying to get a list of all posts from a custom post type called social, and any normal posts within the category social. I currently am using the following:
$posts = get_posts(
array(
'post_type' => array('social', 'post'),
'category_name' => 'social',
'post_status' => 'publish'
)
);
This seems to only be returning posts within the social category, not the social post type. Anyway to return both post_types?
The generated query expects all the conditions to be true: post_typeto be either social or post AND category_name to be social AND post_status to be publish.
One simple solution is to use WP_Query instead:
$query = new WP_Query(array(
'post_type' => array('social', 'post'),
'category_name' => 'social',
'post_status' => 'publish'
));
$posts = $query->posts;
Then, you may use a posts_where filter to modify the where part of the query:
add_filter( 'posts_where', 'my_posts_where', 10, 2 );
function my_posts_where( $where, $query ) {
...modify $where...
return $where;
}
I'll skip the string manipulation part, because there is nothing specific there. Anyhow, $where will look something like this (tested with my WordPress installation):
"AND ( wp_term_relationships.term_taxonomy_id IN (<number>))
AND wp_posts.post_type IN ('social', 'post') AND
((wp_posts.post_status = 'publish'))"
You need to convert it to something like this:
"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>)
AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social'))
AND (wp_posts.post_status = 'publish'))"
Finally, you need to make sure that my_posts_where does not break the other queries. I might just add some indicator to the $query object to identify this particular query in the filter.

how to get latest posts from all custom post types in the site

I have defined multiple custom post types in my wordpress installation. I want to get latest posts from all the custom post types. all resources and tutorials I look into describes only getting latest post from one custom post type, not multiple.
is there any way to do this? for example assigning multiple values to post_type attribute in WP_Query objects or wp_get_recent_posts() function? if answer is yes exactly how to do this.
any help would be appreciated.
It will fetch posts from multiple custom post type.
query_posts( array(
'post_type' => array( 'custom_post1', 'custom_post2', 'custom_post3',
'custom_post4' ),
'cat' => 3,
'showposts' => 5 )
);
First thing I want to clear here I do not know about WordPress but I can help you with the SQL query
I assume you have date time column in your table .
select * from table name
where column name in (here write all your different types followed by comma )
order by your date time column name desc;
E.g.
select * from posts where type in(1,2,3,4) order by created_on desc;
Let's fetch your all custom post types.
$args = array('public' => true, '_builtin' => false);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types( $args, $output, $operator );
The $post_types is now an array containing all the custom post type names. Your all posts query should be like this
$allposts = array( 'posts_per_page' => -1,
'post_type'=> $post_types,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query( $allposts );
if ( $query->have_posts() ) :
while ($query-> have_posts()) : $query -> the_post()
the_permalink();
the_title();
the_content();
endwhile;
endif;

Wordpress query_posts with ID and slug combination

is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks
If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!

Resources