I have a case in which I have 2 custom post types, let's say they are projects and teams. Using Advanced custom fields, I have a relationship field while creating a project and I'm assigning which teams are working on it. (Important: there is no custom field in the teams post type). Later on, I want on single-team, to list all the projects that this team was working on.
The example that Advanced custom fields has is where you have a custom field in the team, not in the project, and I need to do the opposite. (Here is how acf documentation is https://www.advancedcustomfields.com/resources/querying-relationship-fields/).
I tried doing this, but it doesn't work, it says that I don't post a correct data.
$team_id = get_the_ID();
$posts = get_posts(array(
'post_type' => 'projects',
'orderby' => 'teams',
'post__in' => $team_id,
));
Since the ACF relationship field is located in your Project custom post type in your single-team.php file where you want to list of the projects on which the team member worked on you can do the following:
$args = array(
'post_type' => 'PROJECTS CPT NAME HERE',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ACF RELATIONSHIP FIELD NAME HERE',
'value' => get_the_ID(), // the ID of the member
'compare' => 'LIKE',
)
)
);
Related
I have built a search a form at custom page in wordpress and want to filter product on shop page using meta keys that are already exists into posts table.
Initially I have tried to filter categories at form page like this but it doesn't work.
$meta_query = array(
'key' => '_years',
'value' => '2009'
);
$args=array(
'meta_query' => $meta_query,
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'paged' => $paged
);
wc_product_dropdown_categories($args);
The meta_query parameter must be change in an array of array for a single custom field handling:
$meta_query = array(
array(
'key' => '_year',
'value' => '2009',
'compare' => '>',
)
);
Some details from WP_Query page in the Single Custom field handling part
Hope it will work with this.
When there are a lot of variations and you want to edit your stock per variation, the list of variants is not logical (in the admin). When you want to edit 1 variation you have to search the whole list (250+) to find it.
I found a solution here;
Change alphabetical sorting of attributes / variations on product page in Woocommerce shop
But when I edit a bulk edit a price and save the product, only the first 41 are updated. It is because of this line;
'orderby' => 'menu_order',
This is my code;
$args = array(
'post_type'=>'product_variation',
'post_status' => array( 'private', 'publish' ),
'posts_per_page' => -1,
'meta_key' => 'attribute_pa_kleur', //rename with your attributes
'post_parent' => $post->ID,
'meta_query' => array(
array(
'key' => 'attribute_pa_kleur' //rename with your attributes
),
)
);
How can I fix this?
I have a template that pulls a custom post type for properties and sorts them by state. The script works properly until you add in a meta_query for the agent. It pulls the correct properties, but it doesn't sort them by state with the meta_query present. Here's the query:
$qry = array(
'post_type' => array( 'practices-tpsg' ),
'meta_key' => 'wpcf-practice-state',
'orderby' => 'meta_value',
'order' => 'asc',
'showposts' => 18,
'paged' =>$paged
);
if($_GET['agent'])
{
$qry['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-agent',
'value' => $_GET['agent']
),
array(
'key' => 'wpcf-agent2',
'value' => $_GET['agent']
),
);
}
Does anyone know why it would stop sorting properly after the meta_query is added to the query?
Here's a link to the page: http://www.totalpracticesolutionsgroup.com/practices-for-sale/ The sort order works properly in the default view, which allows the state name headers above the listings. If you select an agent from the drop down in the top right, the correct properties are pulled, but they are no longer sorted by state. You can also see the output of the query by adding &debug=1 to the end of the url.
replace 'orderby' => 'meta_value' with 'orderby' => 'meta_value_num
i have one event plug-in and now i want to show my event post like current events,upcoming events then past events.i have created one custom post for this and create 2 meta-boxes for start date and end date. But now how i call?
Without more details, I just can say you'll probably need to perform a query similar to this one:
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'end_date',
'meta_query' => array(
array(
'key' => 'endt_date',
'value' => time(),
'type' => 'numeric',
'compare' => '<'
)
)
);
$past_events = new WP_Query( $args );
This would return the past events, assuming your post type is called events, you have a custom field end_date that stores a numeric timestamp...
You should modify the meta_query accordingly for current and past events, using different value and compare arguments.
Take a look at docs for WP_Query class and in particular to the Custom Field Parameters section.
I have a custom post type called portfolio and a custom taxonomy called build-type (acting as categories)
I am trying to query portfolio posts by build-type ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id'
),
'orderby' => 'title',
'order' => 'ASC'
));
Currently it's calling all portfolio posts and not just those with the build-type ID
For 'field' => 'term_id' should I be using term_id, tag_ID, id or something else?
Anyone know how to get this working?
Thanks in advance!
I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query needs to be an array of arrays
The final solution is:
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
On github here:
https://gist.github.com/1275191
I'm not a WP-gury and I have invested hours and hours trying to solve the same problem. Eventually I found this blog post: http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
The answer is somewhat semi-bad: apparently you can't filter like this for custom post types (it is only possible for posts), which is a shame!
What I did work was this:
$args['custom_tax'] = 'custom_tax_slug';
query_posts($args);
Hope it helps!
//Mike