WP_Query with “post_title LIKE 'something%'” and category - wordpress

I need to do a WP_Query with a LIKE on the post_title and category
This query_post is not working
query_posts(
array(
'post_type' => 'add_buying',
'like' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing'
));

Check this url and change the like parameter.
query_posts( array(
'post_type' => 'add_buying',
's' => $keywords,
'posts_per_page' => 5,
'taxonomy' => 'add_country',
'term' => 'drawing'
));

Change your second parameter to 's' and I think it's going to work:
$args = array(
'post_type' => 'post',
's' => $search_term,
'post_status' => 'publish'
);
$wp_query = new WP_Query($args);
And good luck

title (string) - use post title (available with Version 4.4).
$args = array(
'post_type' => 'post',
'title' => $title,
'post_status' => 'publish'
);
$wp_query = new WP_Query($args);

Related

LIKE query not returning data as expected - WordPress

I really don't know what's the issue. I think my code is OK but the output is wrong. I don't know anything about WordPress, please help me.
elseif ($_GET['search']) {
$args = array(
'post_type' => 'head_to_toe_videos',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $_GET['search'],
'compare' => 'LIKE',
)
),
'posts_per_page' => 12,
);
}
Your query is correct but need to execute you query like this:
$the_query = new WP_Query( $args );
$result = $the_query->get_results();
echo "<pre>"; print_r($result); exit;
Try the below code your problem will solve.
elseif ($_GET['search'] != '') {
$args = array(
'post_type' => 'head_to_toe_videos',
'post_status' => 'publish',
'meta_key' => 'post_title',
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $_GET['search'],
'compare' => 'LIKE',
)
),
'posts_per_page' => 12,
);
$result = new WP_Query( $args );
}

In WordPress post per page is not working when use exclude ids from the query

I have few feature post which i have to exclude from all post so I have used post__not_in but it return all post instead of posts-per-page 6 post how to fix this isssue.
$exclude_ids = array(1,2,3,4,5,6,7,8);
$args = array(
'post_type' => 'post',
'post__not_in' => $exclude_ids,
'orderby' => 'publish_date',
'order' => 'ASC',
'posts-per-page' => 6
);
$latest_post = new WP_Query( $args );
Maybe its just a typo but the correct param is "posts_per_page" not "posts-per-page" (dash vs underscore) (see here https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters)
$exclude_ids = array(1,2,3,4,5,6,7,8);
$args = array(
'post_type' => 'post',
'post__not_in' => $exclude_ids,
'orderby' => 'publish_date',
'order' => 'ASC',
'posts_per_page' => 6
);
$latest_post = new WP_Query( $args );

How to query specific custom post with custom fields in a post_type?

I'm new into wordpress and kinda confused how to get 1 post in a custom post. I only knew the loop where it echo all the content in a post_type.
I'm aiming to get 1 post from a post_type 'product-category' and the meta_key is 'product-category-and-type'
You could try this query for custom in wordpress:
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
"numberposts" => 1,
'meta_query' => array(
array(
'key' => 'product-category-and-type',
'value' => 'meta_value'
)
)
);
$getPosts = new WP_Query($args);
Just copy and paste below mentioned code at your desired position and replace "your_meta_value" with your actual meta value for meta key "product-category-and-type".
You will get your expected result :
<?php $args = array(
'post_type' => 'product-category',
"numberposts" => 1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'product-category-and-type','value' => 'your_meta_value'))
);
$myposts = new WP_Query($args);
while($myposts->have_posts()) : $myposts->the_post();
the_title();
endwhile;?>
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
'posts_per_page' => '1',
'tax_query' => array(
array(
'taxonomy' => 'product-category-and-type',
'field' => 'slug'
),
),
);
$result = new WP_Query($args);

WP_query twice on a single taxonomy

I have a simple WP_query on a custom post and works fine:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => 'london'
);
but I want it to query two locations so used an array:
$args = array(
'post_type' => 'post-type',
'showposts' => 2,
'location' => array('london','paris')
);
but it only pulls in the last term. Is there another way that I should be doing this?
$args = array(
'post_type' => 'post-type',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'slug',
'terms' => array('london','paris')
)
)
);
Try this

To display the post order by modified date in wordpress

I have to display the posts oder by last modified date. So I used the code bellow.
$args = array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
'order'=> 'ASC',
);
$the_query = new WP_Query( $args );
But I could not find any update in the above code. Should i use something else instead of 'orderby' => 'modified' in the argument.
You should use DESC for order.
Try this:
$the_query = new WP_Query( array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
'order' => 'DESC',
));
Using DESC will give you the latest post first(descending order).
EDIT:
As Andrew commented, the default value for order is DESC and can thus be omitted from the code:
$the_query = new WP_Query( array(
'post_type' => $post_type,
'numberposts' => '2',
'orderby' => 'modified',
));
Try
<?php query_posts($query_string . '&post_type=$post_type&orderby=modified&order=desc'); ?>

Resources