Show first 10 custom post type posts that meet ACF value - php

I've got a custom post-type set up called 'Services' and within that is an Advanced Custom Field called 'completed' which is radio button returning either 'inc' or 'com'
The issue I've got is that when the below code runs it grabs the first 10 posts regardless of whether or not they meet the meta query I've got below
Essentially what I'm looking for is the first 10 posts that are 'inc' to display on the page
$tArgs = array(
'post_type' => 'services',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'completed',
'value' => 'inc',
'compare' => 'LIKE'
),
),
);
I would expect this to loop over the 'Services' post type and only return the first 10 posts that have the value 'Inc' for the completed ACF

Related

Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?

TLDR - After manually updating a custom post, my WP_Query will change it's sorting order and move that updated post to the end of the array response.
I have a custom post type called Events that use Advanced Custom Fields. I have written a shortcode that uses WP_Query to find all the Events that are Open and then sort them by their final_deadline first and their priority second.
Here are the arguments for my query:
$today_is = date('Y-m-d', time());
$open_events_args = array(
'post_type'=> 'events',
'offset' => 0,
'meta_query' => array(
'relation' => 'AND',
'final_deadline' => array(
'key' => 'final_deadline_date',
'value' => $today_is,
'compare' => '>=',
),
'open_value' => array(
'key' => 'is_open',
'value' => 'Open',
'compare' => '=',
),
'priority' => array(
'key' => 'event_priority',
'compare' => 'EXISTS',
)
),
'orderby' => array (
'final_deadline' => 'ASC',
'priority' => 'ASC',
),
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
);
$the_open_query = new WP_Query($open_events_args);
$open_events = $the_open_query->get_posts();
Many of my advanced custom fields are being set programmatically through an API call. What I found was the WP_Query initially returned correct results, but as soon as I manually updated an event post (without changing any of the fields), that post would move to the end of the $open_events array and be out of order.
If I updated another post, it too would move to the end of the array. Eventually the open_events array was bifurcated. The first section had events that were not manually updated, and the second section had events that were updated ... and each section was in the correct order.
I went ahead and manually updated my 17 events and figured that would fix it. However, now that I'm adding images to a new advanced custom field, this process is starting all over again. Updated events are moving to the end of the array and out of order.
Does anyone know what would cause this and how I can fix it? Updating an unrelated custom field should not be affecting my WP_Query, but it keeps happening and I'm not sure why.
Thanks.

WP custom post type query does not return custom taxonomy

I have the following CPT:
array(
'slug' => 'articles',
'single_name' => 'Article',
'plural_name' => 'Articles',
'menu_name' => 'Articles',
'description' => '',
'dashicon' => 'dashicons-media-default'
),
and the following custom taxonomy which shows up under the articles CPT:
array(
'slug' => 'author',
'single_name' => 'Author',
'plural_name' => 'Authors',
'menu_name' => '→ Authors',
// This is where you sync the taxonomy to the post types above
'post_type' => ['articles', 'news']
),
Now, I am trying to create a single-articles.php in which I query a single article, to include all custom taxonomy information.
I've tried all variations of:
$args = array(
'post_type' => 'articles',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug'
),
),
);
I am relatively new to PHP & WP and am not 100% sure how to retrieve the data. In Javascript you would get an object back through which one can traverse. When adding print_r($my_query) I don't get what back what I am looking for. Earlier in the document I already initiated the loop:
if (have_posts()) :
while (have_posts()) : the_post()
but I can't figure out why php/wp doesn't show all data included in the object. Ideally, I want to query only the single article and get all data back, to include info about all attached taxonomies. I do not want to query articles BY the taxonomy So I have two questions:
1) How do I query this correctly to get results back?
2) How do I display this data on the frontend?
Edit: According to this tax_query is ignored when is_singular() is true. print_r( is_singular() ) results in 1 (aka true?!), would this make a difference?
I also have custom post types attached to the taxonomies. I need to retrieve this info as well.
Your taxonomy slug is author NOT authors
$args = array(
'post_type' => 'articles',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => 'your-term-here',
),
),
);
You can Use single-{posttype}.php for the single template.
And, if you have register your post type with the has_archive argument set to true, then you can use archive-{posttype}.php for your archive template.
You can check Template Hierarchy

WP Query argument to search by post title

I am trying to filter my posts by post title. I can use the code below perfectly to filter by anything in postmeta, but i want to get results that match against the post title. How do i change this so it searches wp_posts as opposed to wp_postmeta? I want to search purely by title of the post.
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $keywords,
'compare' => 'LIKE'
)
)
Not best solution but it works!
$args = array(
'posts_per_page' => 5,
's' => $keywords,
);

Wordpress sort by number custom fields gives different results

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

filter get_posts with ACF fields, nested

I have several post types:
Products
Reviews
Showcases
Manufacturers
Those are the two most important for this question
Reviews and Showcases are about a product, so when an user adds a review or showcase, they have to select the product trough an ACF relationship field.
When a user adds a product, they have to select the Manufacturer trough an ACF post object field
I have created all my custom pages for the reviews, product and showcases, now I arrived at the Manufacturer post type.
What I want here and am unable to achieve is show the latest 5 reviews, products and showcases with this manufacturer.
I know how to create a query etc, but have no idea what arguments to set in order to filter reviews and showcases (they work the same way, two levels nested) and products (one level nested) for that specific manufacturer.
Can somebody please post me in the right direction?
When you create an Query, you can ask for different post types in a query with givin an array of post types:
$args = array(
'post_type' => array( 'post', 'page', 'movie', 'book' )
);
$query = new WP_Query( $args );
In addition to that you can make a meta_query within this WP_Query in order to ask for the associated manufacturer:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'orange'),
'compare' => 'IN',
),
array(
'key' => 'featured',
'value' => '1',
'compare' => '=',
),
),
));
See the docs here.

Categories