I’m using oxygen builder to create a new WordPress e-commerce website but in truth I think that this question is sufficiently generic to not be linked specifically to oxygen.
The website will sell fish and I’m trying, on each product page, to display any related Custom Post Types (of type recipes) that I have created. Each Product and each recipe share a common taxonomy (featured_fish). PHP is not a strong point, truthfully I’m much more conversant with .Net, so I’m a little out of my comfort zone.
I’m pretty sure that I need to use wp_query()
to retrieve my posts and I think that get_the-terms() will be the right thing to pull the featured-fish taxonomy from the product.
To that end I have the following;
<?php
function dynamic_fish_query($query){
/* this code is being run on a woo commerce single product template,
and I know that the $terms variable below has a value*/
$terms = get_the_terms( $post->ID , 'featured_fish' );
/* I now wish to build a query to return some posts from a custom post type
and show these posts on my woo commerce single product page, the woo commerce products are fish
and the custom posts are fish recipes. The products and custom posts share a common taxonomy
featured_fish */
$args = array(
'post_type' => 'recipes',
'tax_query' => array(
array(
'taxonomy' => 'featured_fish',
'terms' => $terms[0]->name,
),
),
);
$query = new WP_Query( $args );
}
add_action( 'pre_get_posts', 'dynamic_fish_query' );
?>
If I try to save this I get a 503 error.
I think that my basic premise may be correct but my execution clearly isn’t. I’d welcome any suggestions and especially an insight into why the 503 error occurs.
2
Answers
You code is going in a loop result in Memory Issue.
You have used the filter in a wrong way.
If you need to show the posts based on specific query don’t use the filter you have used.
Simply paste this code where you would like to display the posts based on the query
You need to know the file hierarchy for single page/post.
If you need to display this data on single product page try to access related single product php file and put this code over there.
and loop data through
$query
Fires after the query variable object is created, but before the actual query is run.
add_action( 'pre_get_posts', 'dynamic_fish_query' );