Retrieve CPT Posts from a specific category | WordPress - php

I am currently retrieving posts from a custom post type using the following array:
$args = array(
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'location',
'value' => get_the_ID(),
'compare' => 'like',
)
)
);
$events_list = new WP_Query($args);
It works great.
Now, I was wondering if I could retrieve these same posts but ONLY if they appear in the category ID '6'.
Can someone explain how I update my current code to allow this?
Thank you.

Use 'cat' instead of 'category':
$args = array(
'post_type' => 'event',
'cat' => 6,
'meta_query' => array(
array(
'key' => 'location',
'value' => get_the_ID(),
'compare' => 'like',
)
)
);
$events_list = new WP_Query($args);

Related

woocommerce, product link by reviews loop

I created the very simple loop here, the aim is to extract only reviews that have a value equal to or greater than 4 stars. And so far so good. Now from this loop I would also like to get the link to the specific product. Anyone have any idea how to get this information?
$reviews = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => array('4','5'),
) ),
) );
foreach( $reviews as $review) {
/* stuff */
}
Working on it a little longer, I gave myself the answer, I attach the solution to my problem:
<?php
$reviews = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => array('4','5'),
) ),
) );
foreach( $reviews as $review) {
$postId = $review->comment_post_ID;
the_permalink($postId);
}
?>

Wordpress - Count all posts that have a future date to the current one

I'm looking for there to count all the posts that are there in future date to the current one
Query:
<?php
$mostra_data_corrente = date('d-m-Y');
$query = new WP_Query( array(
'post_type' => get_option('customer_postquery'),
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'metakey_AMC_data',
)
),
'date_query' => array(
'after' => $mostra_data_corrente,
),
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'field' => 'slug',
'terms' => $queried_object,
)
) ) ) ;
$conta_risultati = $query->found_posts;
echo $conta_risultati;
?>
Where:
get_option('customer_postquery'): dynamically retrieves all the custom post types that have been created
metakey_AMC_data: is the meta key where the date of the event (post) is enclosed within the meta_value
$queried_object: dynamically retrieves the taxonomy of posts based on the page we are on, thus filtering the posts based on their taxonomy
So my intent is to count all the posts that are there that have a future date to the current one
"It does not count perfectly how many posts exist at a future date compared to the current one"
edit code:
<?php
$mostra_data_corrente = date('d-m-Y');
$query = new WP_Query( array(
'post_type' => get_option('customer_postquery'),
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'metakey_AMC_data',
'compare' => '>',
)
),
'date_query' => array(
'after' => $mostra_data_corrente,
),
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'field' => 'slug',
'terms' => $queried_object,
)
)
) ) ;
$conta_risultati = $query->found_posts;
echo $conta_risultati;
result:3 , but it's not the truth because i have 7 result after current date for this taxonomy
May be you need to query them like :
$currentPostDate = 'get your post date here';
$args = array(
'post_type' => 'post',
'meta_key' => 'the_date',
'meta_query' => array(
array(
'key' => 'the_date',
'value' => $today,
'compare' => '>='
)
),
);
$your_custom_query = new WP_Query($args);
$postcount = count($your_custom_query);
Think to make it as function in your functions.php file.

WP Query filter by ACF field don't work

I'm having an issue with WP query filtered by ACF Post Object Field.
I have to query the 'post' filtered by 'author' acf field.
i'm using this code but this don't work
$post_type_query = new WP_Query(
array (
'post_type' => 'post',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova'
)
)
)
);
Thereis one article on wordpress post with 'prova' author, but the query return empty.
I can't understand why
Thanks
Try this:
$postData = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 3,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'author',
'value' => 'prova',
'compare' => '=' // or if you want like then use 'compare' => 'LIKE'
)
)
)
);
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "Post Title";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;

Fetch WordPress posts and order by meta_key's ID

I am trying to fetch the post by meta_key in WordPress, and now I want to order posts by the meta_key's ID.
My code:
$args = array(
'post_type' => 'post',
'posts_per_page' => 15,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'on',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
I want to order them based on the time meta_key was created. How can I accomplish that?

meta_query not working in wp_query

My post type is product. I use a checkbox field with meta key is ht_featured, meta value when I print_r is array([0] => featured).
My WP_Query:
$the_query = new WP_Query(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
);
It doesn't show any post.
I tried with value => 'featured' and 'compare' => 'EXISTS' but it not working.
WP_query needs to be passed in an array. use following code and let me know if any prob.
$the_query = new WP_Query (array (
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
You can refer to the discussion at wordpress forum:
http://wordpress.org/support/topic/how-to-wp_query-meta_query-value-string-contain-in-key-string
You're passing all of this into WP_Query as individual arguments when they should be contained in an array.
$the_query = new WP_Query( array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN',
),
),
) );
Can you clarify your point about the checkbox? I'd suggest simply updating 'ht_featured' with either 'yes' or 'no' when you save the product. Then change your 'value' in the meta query to 'yes' and remove the 'compare'.
Are you sure there is no php error?
I think WP_Query needs to be passed in an Array
$the_query = new WP_Query(
array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
I had a similar problem until I used the function get_posts() rather than creating a new WP_Query. See if that helps...

Categories