Wordpress wp_query exclude word and category - wordpress

I am trying to exclude word 'Vape' and exclude category using wp_query. Is it possible?
$args = array(
's' => '-Vape',
'cat' => '-62',
'posts_per_page'=>'16',
'paged'=>$paged,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
I use the code above but its not working. I don't get the items that have in their title the word 'Vape' which is right but i get the items of the category 62...any help appreciated!

Try with category__not_in:
$args = array(
's' => '-Vape',
'category__not_in' => array( 62 ),
'posts_per_page'=>'16',
'paged'=>$paged,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
Edit: category__not_in expects an Array to be passed.

Related

Get users list by published post count in last month ago

I want get lists of users by published posts count in a month ago.
I have this code but dosnt work, just show registered users in a month ago
<?php
$args = array(
'orderby' => 'post_count',
'order' => 'DESC',
'role' => 'Subscriber',
'number' => '4',
'date_query' => array(
array(
'after' => '12 hours ago',
'inclusive' => true,
),
),
);
$user_query = new WP_User_Query( $args );
Try this code
Change query array to this
$post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_count',
'order' => 'DESC',
'posts_per_page' => 4,
'date_query' => array(
array(
'after' => '1 month ago'
)
)
);
$post_query = new WP_Query($post_args);
then in a loop use the_author() function to show your users lists by published post count
if ($post_query->have_posts()) {
while ($post_query->have_posts()) {
$post_query->the_post();
the_author();
}
}
I think your problem is that you are trying to do two types of query in one go.
Remember that this query is for users - the date_query is applied to user query and not posts.
You need to retrieve authors with posts first and then based on their ID you can retrieve their posts with post query.
I think this should look something like this (not tested and please correct me if wrong):
$args = array(
'orderby' => 'post_count',
'order' => 'DESC',
'role' => 'Subscriber'
);
$user_query = new WP_User_Query( $args );
$authors = $user_query->get_results();
if (!empty($authors)) {
foreach ($authors as $author){
$author_info = get_userdata($author->ID); //user data
$author_id = $author->ID;
$args = array(
'author' => $author_id,
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 10,
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
$the_query = new WP_Query($args);
//post loop here
}
}
The above code should get all Subscribers and retrieve their ID's, for each user there is another query based on their ID to retrieve their posts.
Source 1: https://wordpress.stackexchange.com/questions/109710/get-posts-get-all-posts-by-author-id
Source 2: https://developer.wordpress.org/reference/classes/wp_user_query/
Source 3: https://wordpress.stackexchange.com/questions/99265/display-posts-of-the-last-7-days

Get 5 random posts within current category in WordPress

I want to only get 5 posts within current category by random using the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
) );
How do you apply the '5 posts' limit and 'order by random' to the above code?
as for me, I would use get_posts(), but these arguments should work in your case as well:
<?php $args = array(
'numberposts' => 5,
'category' => $categories[0]->term_id,
'orderby' => 'rand',
'exclude' => array( get_the_ID() ),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
More about this here: https://codex.wordpress.org/Template_Tags/get_posts
I used the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'orderby' => 'rand',
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
) );

Use 'LIKE %word%' for ACF Query Wordpress

I use ACF plugin on my wordpress site and I want to code custom research.
I have many products and I want to display products which contain the searched word.
I do my query like this :
$args = array(
'post_type' => 'product',
'meta_key' => 'brand',
'meta_value' => $word,
'compare' => 'LIKE');
$the_query = new WP_Query($args);
But this display only products with the brand which exactly matches with $word.
for exemple if I search "yan" I want to display products with the brand "YANMAR", "POLYAN", "TRYANPO", etc.
How to do this please ?
Thank you and have a good day !
Try below code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'brand',
'value' => $word,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args);

WP_Query Search is not working.....for keyword 'TATTOO'

This search is not working.... Any guess?? This is a very simple piece of code.
It is for showing records related with keyword tattoo for title field. It shows all products. The search is not triggered.
$args = array(
'posts_per_page' => $per_page,
'offset' => $paged,
'post_type' => 'product',
'meta_key' => $orderby,
'orderby' => $num_orderby,
'order' => $order,
'search' => '*tattoo*',
'search_columns' => array('post_title')
);
$new_query = new WP_Query( $args );`
http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
try use 's' instead 'search'
...
's' => '*tattoo*',
...

Obtain post by id without order in wordpress

I am try to get this page by ids like this.
$args = array(
'post_type' => 'page',
'post__in' => array(2,1220,3731,696,1899,380)
);
$query = query_posts($args);
this is ok, but I obtain the post by order first 3731 and last 2, buy I don't want any order I want this order, 2, 1220, 3731, 696... etc
any idea!!
add the orderby parameter
$args = array(
'post_type' => 'page',
'post__in' => array(2,1220,3731,696,1899,380)
'orderby' => 'post__in',
);
$query = query_posts($args);
CODEX
Hi #Mark thanks for all I try this too but no work.
Finally I found the solution!
$args = array(
'post_type' => 'page',
'meta_key' => 'ordenacion',
'orderby' => 'meta_value',
'order'=>'ASC',
'post__in' => array(2,1220,3731,696,1899,380)
);
$query = query_posts($args);
I create a field custom and order by the value of meta_key

Resources