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

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

Related

WP_Query 'post__not_in' condition is not working: shows all posts

thank you for taking the time to review this.
I am calling a new WP_Query with the following arguments:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($featured_post_ID),
'category_name' => 'blog',
'offset' => $offset,
'posts_per_page' => 3,
'orderby' => 'post-date',
'order' => 'DESC',
);
I know that past issues have been caused by passing a numeric post ID as a variable instead of string; to get around that I am using the following variable definition for $featured_post_ID:
$featured_post_ID = '"'.$post_ID.'"';
When I run this query, it outputs the post I am trying to exclude.
Here is a Transcript of output of the WP_Query to save space. Looks like post__not_in is recognized in the query, but has no effect on the output.
Can anyone advise why this might be happening, or how to fix it?

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.

Wordpress get_posts() offset by finding index of post by slug

I'm building a Wordpress page which aggregates all posts of a type (press-post).
I want to be able to pass in a URL parameter of a post, which will offset the query to start at the index of the post slug.
For example, www.mysite.com/press should start at offset 0, where as www.mysite.com/press?post=third-post-slug should query at offset 2.
How can I find the offset of a post in a query by its slug?
Here is my code so far:
if ($_GET['post']):
$post_offset = // Here I need to get the offset of that post in the query;
else:
$post_offset = 0;
endif;
$args = array(
'post_type' => 'press',
'post_status' => 'publish',
'posts_per_page' => 15,
'offset' => $post_offset,
'paged' => $page_no,
'orderby' => 'date',
'order' => 'DESC',
);
$press_posts = get_posts($args);

Exclude post_type or pages

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.

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.

Categories