0

I'm working to build a food blog and I'd like to add the feature that permits to logged users to save in a specific area all favorite recipes. I created two specific post types (likes and recipes) and a custom field that makes the association (favorite_recipe_id) and control if the user has already added that specific recipe in his list.

Now I'd like to create the area to show to the user all the recipes added in the list: I'm a little bit confused 😕, anyone should help me to find a right way to follow?

Thanks a lot 👍

4
  • My guess would be you want to perform a WP_Query meta_query of sorts. To get more precise help it is best to be more specific, optimally with some code. Feb 9, 2018 at 16:13
  • Sorry, I try to explain better: - Step 1: a user visits a specific recipe, click on star icon and, with a ajax call to rest API, add it to his hypothetical list. new WP_Query(array( 'author' => get_current_user_id(), 'post_type' => 'like', 'meta_query' => array( array( 'key' => 'favorite_recipe_id', 'compare' => '=', 'value' => get_the_ID())) )); - Step 2: I'd like to create a page to show all the recipes that a user added with star icon click. I'm trying to run some query to understand if I get the right results. In my idea the query can be something like: ...
    – Ale
    Feb 9, 2018 at 17:57
  • ... SELECT *FROM wp_posts JOIN wp_postmeta WHERE post_type = 'like' AND meta_value = 'favorite_recipe_id' AND post_author = 1 (to get all my likes (ADMIN); ...but I'm sure that isn't the right approach, thanks for your help @Nicolai
    – Ale
    Feb 9, 2018 at 17:58
  • Please add additional information to your question by editing it. Feb 9, 2018 at 18:00

1 Answer 1

1

Data associated to a user can be saved in user meta. Save the recipe post IDs in an array for the user.

$my_favs = array( 42, 23, 99 );
update_user_meta( get_current_user_id(), 'user_favs', $my_favs );

Then to query all recipe posts with those IDs, pass the array as the post__in argument to WP_Query:

$my_favs = get_user_meta( get_current_user_id(), 'user_favs', true );
$args = array(
    'post_type' => 'recipe',
    'post__in'  => $my_favs,
);
$recipes = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.