Exclude post_type or pages - php

I am trying to exclude a specific post_type or pages, but I am not sure if I am thinking correctly. The issue is that all my pages comes up in my while query which is supposed to be dedicated for (almost) all my posts.
Here is what I am dealing with:
$args = array(
'post_type' => 'any',
'posts_per_page' => '-1',
'post_taxonomy' => 'any',
'cat' => -14,
);
I was thinking about writing 'post_type' => 'any' into posts, but as I remember that didn't work with my custom posts.
Do anyone have a working solution?
Thanks

If you are talking about filtering post with WP_Query you should read something here https://codex.wordpress.org/Class_Reference/WP_Query
Basically you can use a lot of filters, for example:
$args = array(
'post_type' => 'post',
'cat' => -14,
'post__not_in' => array( 2, 5 )
);
$query = new WP_Query( $args );
This will find only posts (not pages), not in category with id 14 and not those with post ID 2 or 5.
Now if you could be more precise in your question I could give you the exact array you need to obtain it.

Related

WP_Query by an array or list of post slugs

Is this possible, I couldn't find the question asked anywhere on the web.
If you had a list of posts by their slug, example:
$postslugs = array("page-one","page-two","page-three");
And you wanted to create a single query to find all these specific pages:
$args=array(
'post__not_in' => array($post->ID),
'posts_per_page'=> -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'pagename' => $postslugs
);
$my_query = new wp_query( $args );
The result should come back with the three pages. This of course would not work the way it's written now. Is this possible?
If you take a look at the documentation for the WP_Query() class, specifically the Post & Page Parameters section, you'll see it has a handy parameter called post_name__in which takes an array of post_name (aka slugs). Also, are you sure you need to use post__not_in (and maybe posts_per_page since you're defining an array of slugs to get already?
$names = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'post_name__in' => $names
);
$my_query = new WP_Query( $query_args );
I would like to notice, that there is some issue with old WordPress versions where post_name it is not = to a post_slug.
For example if you try to create new post with title Customer.io it will create a post with name customer-io but slug will be customerio the same will be for this Pokéapi
The same will be if you change post slug after you have created the post.
So the best way is to check manually inside loop of your WP_Query.
$desired_array_of_slugs = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish'
);
$my_query = new WP_Query( $query_args );
Then
foreach($my_query as $post) {
if(in_array(sanitize_title($post->name)], $desired_array_of_slugs, true)){
//do ur staff
}
}

Getting custom post type data including ACF with Timber?

I need to get custom post type data for usage with Timber and Twig, it's working fine using the standard WP_Query, such as:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new WP_Query( $args );
// Restore the global $post to the current post in the main query
wp_reset_postdata();
However this obviously doesn't get ACF fields.
Was going to do the below as indicated in the Timber docs:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = Timber::get_posts( $args );
However, it seems that get_posts is getting deprecated in 2.0.
It seems like the best way to do it would be by using Timber's PostQuery, but because of lacking documentation I am unsure if it is more or less an encapsulation of WP_query and I can simply do something like:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$context['testimonials'] = new PostQuery(array('query' => $args));
Am I on the right track here or what is the correct way?
Ok, after finding this answer and also digging through the code a bit I found out how to do it I believe:
// Get the testimonials custom post type posts
$args = array(
'post_type' => 'testimonial',
'post_status' => 'publish',
'perm' => 'readable',
'nopaging' => true
);
$query = new Timber\PostQuery($args);
// Get an array of post objects
$context['posts'] = $query->get_posts();
From what I can tell, all the respective WP_Query args seems to work using this method as well - but if you need pagination to work I would advise reading that other answer I linked to as it contains more information on that.
ACF fields have some weird numerical keys in the database. To get the data just use their own function
get_fields();
https://www.advancedcustomfields.com/resources/get_fields/

post_type not working when category_name is set wp_query

When using post_type together with category_name in wp_query, the filtering for the specific post type stops working. How can I filter by both post type and category name?
When I remove the category_name argument, the filtering starts working again.
$args = array(
'category_name' => 'road,city',
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'posts_per_page' => 2,
'paged' => $paged
);
$query = new WP_Query($args);
I expect to get posts within the categories 'road' and 'city' but also get pages within those categories. The category query works but not the post_type.
The posts_type should be working fine. You may try changing for the category. In my case, I have added custom categories in Wordpress using the below lines -
'category__in' => array($category_id1, $category_id2)
Hope this helps.

WP_Query: How to test for sticky posts before assigning posts_per_page?

Is there any way to 'test' whether there are Sticky Posts?
If there IS a sticky post, I'd like to use 'posts_per_page' => '3' and if not, use 4, as I'm finding that Sticky Posts add to the 'posts per page' which breaks the layout :(
I have a WP_query as follows:
$article_args = array(
'post_type' => 'post',
'posts_per_page' => '3',
'post_status' => 'publish',
'order' => 'DESC'
);
$article_query = new WP_Query($article_args);
Is that possible, maybe to test for this before the args array and use the appropriate number?
Yes, I have considered ignore_sticky_posts but I do want to include them still.
Any advice from someone who knows PHP better than me?! :/
Thanks. Martin.
A sticky post is saved as an option which you can check with $sticky = get_option( 'sticky_posts' );. This returns an array with post ID's. You can simply just check if the option is empty or not, and then modify your arguments accordingly
You can do something like this
$sticky = get_option( 'sticky_posts' );
if( $sticky ) {
$article_args = array(
'post_type' => 'post',
'posts_per_page' => '3',
'post_status' => 'publish',
'order' => 'DESC'
);
}else{
$article_args = array(
'post_type' => 'post',
'posts_per_page' => '4',
'post_status' => 'publish',
'order' => 'DESC'
);
}
$article_query = new WP_Query($article_args);
Unfortunately not, but there probably is another way to achieve what you're after.
You could simply grab the latest sticky post, and show that first, and then, in a second query, grab the rest of the posts (or the next 3, anyway).
That way, you'll get 1 sticky (if it exists) and 3 normal, per page.
We don't know your end goal here, so this could be unusable for you.

Is there a parameter similar "'name__like" for WP_Query?

In the Get Terms(); WordPress function, you can get terms using the "name__like" parameter to only get terms that start with whatever characters you enter.
I need to use a new WP_Query, and I can't find a similar parameter:
$my_qry = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'DESC', 'post_status' => 'publish' ) );
This gives me an array of all the posts published. How can I filter the found posts to only show posts that start with certain words/characters (by post title)?

Categories