Search for posts by title using WP_Query - wordpress

May I know whether the query below is correct? I would like to search for post that contain the title 'hello' using meta_query with various variables
$sq = new WP_Query(array( 'meta_query' => array(array('key' => 'post_title', 'value' => array('%hello%'), 'compare' => 'LIKE')), 'post_type' => 'post', 'posts_per_page' => $num, 'post_status' => 'publish', 'order' => $order, 'orderby' => $orderby, 'ignore_sticky_posts' => 1, 'cat' => $cats, 'offset' => $offset));
Thanks.

get_page_by_title() is usually NOT case sensitive. It only would be if the database was set to be case sensitive, which is unusual for MySQL.
Instructions for use of get_page_by_title() can be found at: developer.wordpress.org
Be sure to set the third parameter if you are wanting posts and not pages.

Related

WP_Query Order By Two Meta Fields

I need to order a WP_Query by two meta fields: meta-order and meta-last-name. First by meta-order numerically, if blank, order by meta-last-name ASC. How do I do that, the documentation is not very clear on that. I've tried this but it does not work.
array(
'post_type' => 'student',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'meta-order meta-last-name',
);
You will need to use the pre_get_posts action to be able to achieve this.
See answers from https://wordpress.stackexchange.com/q/169999 on how to do it.
This worked for me:
array(
'post_type' => 'student',
'meta_query' => array(
'relation' => 'OR',
'custom_order_clause' => array(
'key' => 'meta-custom-order',
'compare' => 'LIKE',
),
'last_name_clause' => array(
key' => 'meta-last-name',
'compare' => '=',
),
),
'orderby' => array(
'custom_order_clause' => 'ASC',
'last_name_clause' => 'ASC'
),
)
CAVEAT:
Make sure you delete empty meta values from DB otherwise this will not work.

Wordpress loop ignore newest post in query

I'm trying to pull out all of the posts that have a meta value of Main, while trying to ignore the newest post created. I've looked here (WordPress site https://codex.wordpress.org/Class_Reference/WP_Query) but I can't figure out a way to ignore the newest post. It can't be by a set date, because it needs to always ignore the newest post.
$args = array(
'order' => 'DESC',
'meta_key' => 'main_story',
'meta_value' => 'Main',
'meta_query' => array(
'relation' => 'NOT IN',
array(
'key' => 'main_story',
'value' => Main,
'posts_per_page' => 1,
'order' => 'DESC',
),
)
);
I thought trying it this way not not get the newest post, but it hasn't worked. Is there an opposite of getting the meta_query, like ignore_meta_query, but you know, that works?
There are a lot of issues with your posted code...but you're looking for the offset parameter of WP_Query.
offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The offset parameter is ignored when 'posts_per_page'=> -1 is used.
$args = array(
'offset' => 1,
'posts_per_page' => 1,
'meta_key' => 'main_story',
'meta_value' => 'Main',
'meta_compare' => 'NOT',
);

Get ALL post types in WordPress in query_posts

I'm using query_posts to get a list of most popular posts. I'm using several custom post types, and instead of including all of them in the query, I would like a query that just gets all of them, also if I create more.
This is what I have:
query_posts(array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => array('posttype1', 'postype2', 'posttype3')
)
);
If I don't include the post_type, it only gets the standard post type, post. Does anyone have an idea?
You can use 'post_type' => 'any' for fetching from all post types. See this documentation. http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters
Note: It is highly recommended to use WP_Query rather than query_posts. https://wordpress.stackexchange.com/a/1755/27998
'post_type' => 'any',
This gets all posts except revisions. https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
So your query would be:
query_posts(array(
'post_type' => 'any',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_type' => array('posttype1', 'postype2', 'posttype3')
)
);
If you want to use get_posts(), the only work around is to use 'post_type' => get_post_types()
Example that returns ALL posts of ANY post type:
$posts = get_posts([
'post_type' => get_post_types(),
'post_status' => 'publish',
'numberposts' => -1,
]);

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 - Filter by Taxonomy

Something weird happening in my query and can't see what's wrong
I am passing the variable THEME from a select list.
THe theme is pulled from the taxonomy THEME
so my code looks like
$thetheme = $_GET['theme'];`
$thetheme is correctly passed from URL
then
$args2 = array(
'tax_query' => array(
array(
'taxonomy' => 'theme',
'field' => 'slug',
'terms' => $thetheme
)
),
'post_type' => array( 'post', 'dvd' ),
'cat' => '31',
'paged' => $paged,
'posts_per_page' => $listitems,
'order' => 'DESC',
'orderby' => 'date',
'query' => $wp_query
);`
The query is working only on some Post, not all of them, and cant understand why.
If I select a post with the Theme "Adventure" for example, it will pull the correct amount of post.
But an other post, in the same category with a different theme, will not be displayed.
This is puzzling me....
HElP!
thx
Don't think you need here to run complete taxonomy query... try this =)
$args2 = array(
'theme' => $thetheme,
'post_type' => array( 'post', 'dvd' ),
'cat' => '31',
'paged' => $paged,
'posts_per_page' => $listitems,
'order' => 'DESC',
'orderby' => 'date',
'query' => $wp_query
);

Resources