I'm working on a new project where registered users can select multiple careers in the User Profile page in wordpress Admin. I have been able to add the check boxes of different careers to the User profile page which registered users can check/select.
At the frontend, I created a page for each of the careers. I want a scenerio whereby if one should go to page named "Programmer" for example, one would be shown all the authors that check "Programmer" in their profile. The authors info like avatar, name and links to all posts would be shown in a loop
I don't know how to go about it. Hoping you guys here can assist with this.
-------------------------------EDITED---------------------------
With the help provided by #AustinWinstanley thus far, here is my code
$user_query = new WP_User_Query( array( 'meta_key' => 'career', 'meta_value' => 'programmer' ) );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
With the above query, any author whose career field's value is programmer is listed. The above code is suitable if the value field for Career is an input field. I don't intend to make career's value to be an input field, rather I want it to be checkboxes where authors can check multiple values.
Since it's not going to be an input field, but rather checkboxes, how do I edit this line to reflect what I want?
$user_query = new WP_User_Query( array( 'meta_key' => 'career', 'meta_value' => 'programmer' ) );
You probably want to query users by meta data. Something like this:
// Set this value by whatever you're searching for
$career = 'programmer';
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'career',
'value' => $career,
'compare' => 'LIKE'
// Or 'compare' => '='
)
)
)
);
$user_query = new WP_User_Query( $args );
$result = $wp_user_query->get_results();
Edit:
For the front end form:
<form method='post'>
<input name="careers[]" type="checkbox" value="programmer"> Programmer
<input name="careers[]" type="checkbox" value="accountant"> Accountant
<input name="careers[]" type="checkbox" value="manager"> Manager
<button name="career-search">Search</button>
</form>
In your plugin or function/includes:
function careers_postback( $query ) {
if( $query->is_main_query() && isset( $_POST['career-search'] ) ) {
$careers = $_POST['careers']
$meta_query = array();
foreach( $careers as $career ) {
$meta_query[] = array(
'relation' => 'AND',
array(
array(
'key' => 'career',
'value' => $career,
'compare' => '='
)
)
);
}
$args = array(
'meta_query' => $meta_query
);
$user_query = new WP_User_Query( $args );
$result = $wp_user_query->get_results();
set_query_var( 'search-career-results', $result );
// Here you would include your search template and call exit
// In your template, use `get_query_var( 'search-career-results')
// Then check if there are results and list them if there are.
}
}
add_filter( 'pre_get_posts', 'careers_postback' );
This is in general. I haven't fully tested it because I'm not typing it in an editor. But this is the full solution to check boxes and get users by their meta data using the data. If this doesn't work in general, then you are doing something you aren't revealing. Like not saving careers to user meta data.
Related
I am building an e-commerce website and I have installed the following plugin (Customer reviews for Commerce - https://wordpress.org/plugins/customer-reviews-woocommerce/) for Reviews and Ratings of Orders once user completes the order process.
However, the nature of the products we deal with (like fabrics, dresses, sarees etc.) will run out of stock and the same product will not be available again to procure. So, I would want to display the reviews and ratings of old orders using the 'tags' of the products which the order had (For this reason, I would like to have review at order line item). Further, the new product page should fetch the reviews and ratings using it's own tags from old orders which had the same tags.
Any guidance would be helpful in this matter!
To approach this problem, first thing to do is to get all tags associated with a given product into an array. And then, WP_Comments_Query needs to be queried with the array of product ids generated in the first step.
Here is a snippet with the above mentioned approach.
function get_reviews_by_tags(){
global $product;
$productid = $product->get_id();
// get all product_tags of the current product in an array
$current_tags = get_the_terms( $productid, 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//get all related product ids mapped by tags array we created earlier
$relatedproductids_by_tags = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $current_tags,
'operator' => 'IN'
)
),
));
// create a wp comment query object as wc uses comments for reviews
$reviews_args = array(
'post__in' => $relatedproductids_by_tags
);
$reviews_query = new WP_Comment_Query;
$reviews = $reviews_query->query( $reviews_args );
if ( !empty( $reviews ) ) {
foreach ( $reviews as $review ) {
echo '<p>' . $review->comment_content . '</p>';
}
} else {
echo 'No reviews found.';
}
}
add_action( 'woocommerce_after_single_product_summary', 'get_reviews_by_tags', 10, 2 );
}
The above code does not consider any modifications being made by the plugin you mentioned in your question. Also, please note that this code is for fetching and displaying reviews as mentioned in your question. This is not for creating reviews.
I have a custom post type setup called 'Artists'. Each Single Artist is a page (artist profile if you wish), has a list of products that are associated with that artist via the relationship field type through Advanced Custom Fields (ACF). I need the products to be displayed within their categories on the artist page. So within Elementor I need to specify a 'Query Filter ID' to simply split the products into categories.
What I have tried so far
I am trying to display only products from a certain category in a list via a custom query as I need to generate a Query ID.
I've been trying a bunch of different ways to do this but now I'm at a loss. The latest code I have is here... what am I missing?
/** Product category 'SOFTWARE' **/
add_filter( 'software_product', 'product_category_software' );
function product_category_software( $variable ) {
$query_args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'software',
),
),
);
return $variable;
}
Answer
You need to use this
https://developers.elementor.com/custom-query-filter/#Using_the_Custom_Filter
arbitrary_name_query_id - the QUERY ID that you fill in the Elementor field,
the code to be placed within functions.php:
add_action( 'elementor/query/arbitrary_name_query_id', function( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = [
'key' => 'acf_key', // put ACF relationship field name
'value' => get_the_ID(),
'compare' => '=',
];
$query->set( 'meta_query', $meta_query );
} );
Regarding to the given code
First of all, as Wordpress Documentation on filters says:
They (i.e. filters) provide a way for functions to modify data of other
functions.
Which means that the filter function, in this case it's product_category_software, receives data that subsequntly modifies and returns it back:
add_filter( 'software_product', 'product_category_software' );
function product_category_software( $variable ) { // receive $variable
/**
* Modify $variable here
*/
return $variable; // return modified $variable
}
In your case product_category_software doesn't modify what receives, but introduces new peace of data $query_args which makes no sence, since it is not going to be returned.
Secondly, the first argument of the add_filter function should be an existent filter name, the software_product is not.
Thirdly, the correct name of the taxonomy of product categories is product_cat.
Getting products from a certain category
Aproach #1 Modfying main query with pre_get_posts
function your_arbitrary_name( $query ) { // receive
if ( ! is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query() ) {
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array('software'),
)
);
$query->set( 'meta_query', $tax_query ); // modify
}
return $query; // and return
}
add_action( 'pre_get_posts', 'your_arbitrary_name', 20 );
Aproach #2 Use wc_get_products or WC_Product_Query
Example, use in your Controller or even in the template:
$args = array(
'category' => array( 'software' ),
);
$products = wc_get_products( $args );
Read the Docs about wc_get_products and WC_Product_Query
I am trying to dynamically populate two dropdown fields in a Gravity Forms form. The first field dynamically populates with the terms available in a custom post type. I want the second dynamically populated field to contain the list of all post titles within the custom post type AND have those titles filtered by the term selected in the previous dropdown. Is it possible to get the value of a dropdown within Gravity Forms and pass that value as a parameter in $args to use the get_posts($args) function?
I started using the following tutorial as a guide. https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
add_filter( 'gform_pre_render_3', 'populate_procedures' );
add_filter( 'gform_pre_validation_3', 'populate_procedures' );
add_filter( 'gform_pre_submission_filter_3', 'populate_procedures' );
add_filter( 'gform_admin_pre_render_3', 'populate_procedures' );
function populate_procedures( $form ) {
// Procedure Category Dropdown
foreach ( $form['fields'] as &$field ) {
The first field. The following code populates a dropdown field containing a list of all of the terms within a custom post type (procedure):
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedure_categories' ) === false ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => 'procedure_category',
'orderby' => 'name',
'order' => 'ASC',
) );
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
//$posts = get_posts( 'post_type=procedure&numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure Category';
$field->choices = $choices;
The second field. The following code dynamically populates the field with all of the the post titles of the custom post type (procedure). I want to filter these results based upon the value selected above.
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedures' ) === false ) {
continue;
}
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => 'cardiovascular',
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure';
$field->choices = $choices;
}
return $form;
}
The second dynamically populated field successfully pulls in the filtered list of post titles based on the $args if I explicitly listed the term (in the example above I used 'cardiovascular'). What I am wondering is if there is a way to grab the value of the previous field and use that to filter the results of the second field (without having to reload the page). Any ideas? Does Gravity Forms have a functionality like this built in?
Using this method, you would need to use multiple pages and add the second Drop Down field to the second page on the form. Then, when the user submits the first page, you can access the value of the first Drop Down from the $_POST. Gravity Forms has a helper method for doing this called rgpost(). Here's what your $args might look like:
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => rgpost( 'input_FIELDID' ),
);
Replace FIELDID with the field ID of your first Drop Down.
With that said, if you want to accomplish this without having to touch any code, try Gravity Forms Populate Anything.
https://gravitywiz.com/documentation/gravity-forms-populate-anything/
I have a "taxonomy" custom field for the user pages. I want to build a query filtered by this field. It works with normal querys but not with user-querys, am i doing something wrong?
<?php
$args = array(
'key' => 'fruits',
'value' => 'apple'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
Try get_users instead:
$users = get_users(array(
'meta_key' => 'fruits',
'meta_value' => 'apple'
));
var_export($users);
Wordpress codex: get_users()
Edit:
After a bit of research it turns out that get_users() is only a wrapper for WP_user_query, so switching to this function will make no difference.
However... did you notice that in my answer (and vrajesh') we have substituted your key with meta_key, and value with meta_value ... They are definitely defined in the WP_User_Query class, so I would be surprised if they didn't have any meaning.
If by chance you are using your original $args (which I guess does not actually refer to fruits and apples), then that may well be the explanation you are getting nothing.
try this:
$args = array( 'meta_key' => 'fruits', 'meta_value' => 'apple','compare' => '=');
Use WP_Query instead of WP_User_Query. WP_User_Query is used to retrieve data from user and usermeta table. And according to my understanding your are retrieving data from posts and postmeta table.
Class Reference/WP User Query and
Class Reference/WP Query
UPDATED:
Try this
<?php
$args = array(
'meta_query' => array(
'key' => 'fruits',
'value' => 'apple',
'compare' => '='
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
I need to modify or create a WP_Query that will search for a search term in both the post title OR a custom field (called 'my_field').
I have been reading and trying for hours, but I am right back to this code (below) which, alas, only searches in 'my_field' and does not take the post_title into account.
function my_pre_get_posts_2( $query ) {
if ( is_admin() && $query->is_main_query() && $query->query['post_type'] === 'post' && isset($query->query['s']) ) {
$search_word = $query->query['s'];
$args = array(
//'s' => $search_word, //If I include this line, the WP query seems to AND post_title and my_field. If I comment out this line, the WP query only searches in my_field. (I need WP to OR post_title and my_field.)
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'my_field',
'value' => $search_word,
'compare' => 'IN'
),
array(
'key' => 'post_title',
'value' => $search_word,
'compare' => 'IN',
)
)
);
//$query = new WP_Query( $args ); //Need to modify the existing WP_Query
$query->init();
$query->parse_query($args);
}
}
add_action( 'pre_get_posts', 'my_pre_get_posts_2' );
The reason I need to do this is because I need to modify the behaviour of the the 'Search Posts' button in the 'All Posts' (admin) page so that whatever the admin user searches for, it will return the posts that have a matching post title OR my_field value.
To do an OR search, I tried merging two separate WP_Query results as shown here - https://wordpress.stackexchange.com/questions/55519/can-i-merge-2-new-wp-queryvariable-s - in guidod's answer. That wasn't a great solution though, and resulted in erratic behaviour.
The correct solution I found was to modify the query using the WP Custom Query as shown in the code (which requires some modifications) here - http://codex.wordpress.org/Custom_Queries .