Support » Plugin: WP Recipe Maker » can you do a WP_Query on recipe term?

  • Resolved berttervoert

    (@berttervoert)


    I know I can do a WP_Query on category_name, but I was wondering if I can do a query on terms and if so, how do I do that.
    For the category I use:

    $myCatQuery = new WP_Query ( 'category_name=' . $my_cat_name . '&orderby=title&order=asc&posts_per_page=20' );

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Brecht

    (@brechtvds)

    What do you mean by recipe term exactly?

    We have courses (wprm_course) and cuisines (wprm_cuisine) which would be queryable the same way, if that’s what you’re referring to.

    Thread Starter berttervoert

    (@berttervoert)

    Yes that is what I’m referring to. I’m working on a searchbar where you can select recipes by course, cuisine or ingredient. The dropdown lists I build from the information stored in the database. The chosen option is then passed on to the redirected page (this part is already working) where the selection of recipes is made according to the passed option.

    Plugin Author Brecht

    (@brechtvds)

    Here’s an example:

    $args = array(
    	'post_type' => 'wprm_recipe',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'wprm_course',
    			'field'    => 'term_id',
    			'terms'    => array( 2, 34 ),
    		),
    	),
    );
    $query = new WP_Query( $args );

    Which would get all recipes in the courses with those IDs. More information about WP_Query can be found here: https://codex.wordpress.org/wp_query

    Once you have the recipes you’ll probably want to use something like this to get the parent post:

    $recipe = WPRM_Recipe_Manager::get_recipe( $post_id );
    $parent_post = get_post( $recipe->parent_post_id() );
    Thread Starter berttervoert

    (@berttervoert)

    Thank you, that’s very helpfull! i’ll get to work with this.

    Thread Starter berttervoert

    (@berttervoert)

    Can I do the same for ingredients, so:

    $args = array(
    	'post_type' => 'wprm_recipe',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'wprm_ingredients',
    			'field'    => 'term_id',
    			'terms'    => array( 2, 34 ),
    		),
    	),
    );

    or should that be something like 'ingredients_query' => array(...

    Plugin Author Brecht

    (@brechtvds)

    You can do the exact same but it should be wprm_ingredient and not wprm_ingredients.

    Thread Starter berttervoert

    (@berttervoert)

    thank you very much!

    Thread Starter berttervoert

    (@berttervoert)

    I got this code so far, but the query doesn’t seem to get any result:

    			// Get the result from the searchbar send with jquery.
    			if ( isset ($_GET['option']) ){
    				$selected = $_GET['option'];
    				echo $selected . '<br />';
    			}
    			if ( isset ($_GET['recipeterm']) ) {
    				$group = 'wprm-' . $_GET['recipeterm'];
    				echo $group . '<br />';
    			}
    			// build query to get all the recipe post id's requested by the searchbar result.
    			$args = array (
    					'post_type' => 'wprm_recipe',
    					'tax_query' => array (
    						array (
    							'taxonomy' => $group,
    							'field' => 'term_id',
    							'terms' => $selected,
    						),
    					),
    				);
    			$tax_query = new WP_Query ( $args );
    			if ( $tax_query->have_posts() ) {
    				while ( $tax_query->have_posts() ){
    					$tax_query->the_post();
    						// Get the recipes inside the current post.
    					$recipes = WPRM_Recipe_Manager::get_recipe_ids_from_post();
    						// Access the first recipe, if there is one.
    					if ( isset( $recipes[0] ) ) {
    						$recipe_id = $recipes[0];
    						$recipe = WPRM_Recipe_Manager::get_recipe( $recipe_id );
    						echo 'result';
    					}
    					wp_reset_postdata();
    				}
    			}
    			else {
                                echo 'no posts found';
                            }
    

    I tried it with a selection that should result in one recipe, but I get the “no posts found”message on screen. Do you have an idea where I go wrong?

    Thread Starter berttervoert

    (@berttervoert)

    I found an error myself:

    $group = 'wprm-' . $_GET['recipeterm']; should be:

    $group = 'wprm_' . $_GET['recipeterm'];

    But still the query returns no result.

    (I added a few lines to the previous code:

    					if ( isset( $recipes[0] ) ) {
    						$recipe_id = $recipes[0];
    						$recipe = WPRM_Recipe_Manager::get_recipe( $recipe_id );
    						echo 'result';
    					}
    					if ( !isset ( $recipes[0] ) ) {
    						echo 'no result';
    					}
    					wp_reset_postdata();
    
    Thread Starter berttervoert

    (@berttervoert)

    I changed my code again, with the suggestion in your first post:

    				while ( $tax_query->have_posts() ){
    					$tax_query->the_post();
    						// Get the recipes inside the current post.
    					$post_id = get_the_ID();
    					$recipes = WPRM_Recipe_Manager::get_recipe( $post_id );
    						// Access the first recipe, if there is one.
    					if ( isset( $recipes ) ) {
    						echo 'result';
    						var_dump ($recipes);
    					}
    					if ( !isset ( $recipes ) ) {
    						echo 'no result';
    					}
    					wp_reset_postdata();
    				}
    			}
    			else {
    				echo 'no posts found';
    			}				
    

    Now the var dump shows that a recipe is found. Do I have to get the parent post to be able to get image, title and summary of the recipe? Or how do I get about that?

    Plugin Author Brecht

    (@brechtvds)

    WPRM_Recipe_Manager::get_recipe( $post_id ); gets one single recipe, not a bunch of recipes. So you’d use $recipe to avoid confusion.

    Since you already have the ID, you can actually just make use of the shortcodes:
    https://help.bootstrapped.ventures/article/83-recipe-shortcodes

    Thread Starter berttervoert

    (@berttervoert)

    I had changed that into $recipe already myself. At first I thought it wasn’t working with the shortodes, but then I saw I forgot to alter a variable in the shortcode. Now it’s working just fine! The code to output the search-result:

    <div class="search-resultgrid-item">
    	<div class="search-rgr-block-image">
    		<?php echo do_shortcode ( '[wprm-recipe-image id="' . $post_id . '"]' ); ?>
    	</div>
    	<div class="search-rgr-block-title">
    		<a href="<?php the_permalink(); ?>"><?php the_title() ?></a>
    	</div>
    	<div class="search-rgr-block-summary">
    		<?php echo do_shortcode ( '[wprm-recipe-summary id="' . $post_id . '"]' ); ?>
    	</div>
    </div>

    (the problem was I copied the code from another template which had the variable $recipe_id in the shortcode. After changing that to $post_id it worked)
    Thanks again for your quick respons!

    Thread Starter berttervoert

    (@berttervoert)

    .

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘can you do a WP_Query on recipe term?’ is closed to new replies.