a_mulg
Forum Replies Created
-
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesAh right. I have added that tag to a standard post which now shows, so the loop is working correctly. I am slightly confused though! This is the archive function I currently have…
function archive($query) { if ( ! is_archive() && $query->is_main_query() ) return; // echo '<pre>', print_r( $query->query_vars, true ), '</pre>'; $tentative = $query->get('actors'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['actors']); } $tentative = $query->get('writers'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['writers']); } $tentative = $query->get('directors'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['directors']); } if ( ! isset( $person )) return; //$query->set( 'posts_per_page', -1 ); $taxquery = array( 'relation' => 'OR', array( 'taxonomy' => 'actors', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'writers', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'directors', 'field' => 'slug', 'terms' => $person, ), ); $query->set( 'tax_query', $taxquery ); } add_action('pre_get_posts','archive'); add_filter('posts_request', function( $sql ) { return str_replace('wp_posts.post_author = 1 AND ', '', $sql ); }, 100 );
I think what I’m not quite understanding is how that function manages to return custom post types for the actors, writers and directors taxonomies but not the standard tag?
The way I currently have it set up, the actors, writers and directors taxonomies are available across all post types, as well as the standard WP tag taxonomy. The post types I use are reviews, feature, trailer and posts. (Those are their slugs).
I also use the categories awards and festivals across all post types, so I can group all posts about awards or festivals and access them via one menu item on the front end. So those categories would ideally need to show all different post types, as would search and author requests.
I hope this is enough information – thanks again for your help and continued patience!
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesOk, this is what I get when I try to view /tag/action. There are definitely multiple posts with that tag so should be plenty to show.
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (50) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
I double checked the tag_ID and it is definitely 50 as shown in the SQL.
- This reply was modified 7 years, 10 months ago by a_mulg.
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesNo, I removed the pre_get_posts add_action() action in the archive.php file. I’m just concerned that I’m also using standard WP tags for things like genres, festivals, award shows and other more generic terms.
For example, when I visit eg. /tag/action/, it’s using archive.php and returning no posts, even though there are numerous posts with that tag. Is that because the function we created is looking for the custom taxonomies only? Or is it a case of just adding the generic tags to the function?
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesAha, I checked my template and the if have posts was missing the while have posts part. I’ve fixed that and it seems to be working perfectly now! Thanks for all your help and patience!
Just wondering now if it’s possible to apply this to a page other than archive.php so I can have a more generic archive.php page as well?
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesAlso, a quick follow-up question: given that I don’t want to use this exact query for archives that don’t relate to a person, how do I go about applying my function to eg. archive_person.php and not archive.php?
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesSo the good news is…it works! It’s now pulling in posts from every taxonomy, regardless of the URL (eg. actors/, writers/, directors/) so that’s working exactly like it should.
However, this wasn’t immediately obvious as for some reason it’s only displaying the first available post, so I had to do a lot of unpublishing/publishing to see what displays and what doesn’t.
Is that something I would fix via the archive function or via the
<?php if ( have_posts() ) : ?>
part of my template? (Apologies if that isn’t related to this topic exactly!)Forum: Developing with WordPress
In reply to: Linking custom taxonomiesSo I think there’s some progress, though it’s still not really working yet! You’re right about the archive page, although I was on archive.php, I had copied the code into it from archives.php – doh! However, I have now reverted back to my original archives.php file but swapped out the query for this:
<?php add_action( 'pre_get_posts', 'archive' ); ?> <?php if ( have_posts() ) : ?>
Now, the interesting part is, when I unpublish any posts featuring Ben Affleck tagged as a writer or director, it returns only one post featuring him as an actor. However, when I republish his director and writer posts, no posts show at all. This is all via the /actors/ben-affleck URL; no posts show at all on /writers/ben-affleck and /directors/ben-affleck, regardless of what is published and what isn’t. Apologies if that is quite confusing but I feel it’s worth mentioning just in case!
I’ve added the amendment to the author removal patch and added in the the debug code. I think what it has returned is the same as before…
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (268) OR wp_term_relationships.term_taxonomy_id IN (1992) OR wp_term_relationships.term_taxonomy_id IN (1993) ) AND wp_posts.post_type IN ('post', 'trailer', 'feature', 'reviews') AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesHmm unfortunately there are still no posts showing. I’ll copy in the code I’ve got in case there’s some issue with placement…
if ( ! is_archive() && $query->is_main_query() ) return; $tentative = $query->get('actors'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['actors']); } $tentative = $query->get('writers'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['writers']); } $tentative = $query->get('directors'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['directors']); } if ( ! isset( $person )) return; $taxquery = array( 'relation' => 'OR', array( 'taxonomy' => 'actors', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'writers', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'directors', 'field' => 'slug', 'terms' => $person, ), ); $query->set( 'tax_query', $taxquery ); } add_action('pre_get_posts','archive'); add_filter('posts_request', function( $sql ) { return str_replace('wp_posts.post_author = 1 AND ', '', $sql ); });
The contents of my archive page are just pasted from the default, with the pre_get_posts added in. I tried removing that line, thinking it might just display the results from one taxonomy as normal, but it still didn’t display posts so perhaps there’s an issue with my archive template? Here is the code…
<?php /* Template Name: Archives */ get_header(); ?> <div id="container"> <div id="content" role="main"> <?php add_action( 'pre_get_posts', 'archive' ); ?> <?php the_post(); ?> <h1 class="entry-title"><?php single_tag_title() ?></h1> <h2>Archives by Month:</h2> <ul> <?php wp_get_archives('type=monthly'); ?> </ul> <h2>Archives by Subject:</h2> <ul> <?php wp_list_categories(); ?> </ul> </div><!-- #content --> </div><!-- #container --> <?php get_footer(); ?>
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesYep, the taxonomy_term_id, term_id and slug all match up for all three IDs.
I’ve checked the posts_where_request and _posts_clauses_request and they seem to be untouched. There’s nothing else hooked into them that I can see. Every post on the site at the moment is by author 1 but that won’t always be the case and I haven’t set anything to restrict it to author 1, at least not on purpose!
I’ve searched my theme for instances of author and the only ones that appear are
<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>
and<?php the_author(); ?>
, which I’m using across a few different template files. I can’t see anything untoward in my functions file either.I’ve also tried disabling plugins, however there are two that I need to keep active or my custom posts and taxonomies will stop working. With the others disabled, the author still displays in the query.
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesOk cool, I’ve done that and got the following…
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (268) OR wp_term_relationships.term_taxonomy_id IN (1992) OR wp_term_relationships.term_taxonomy_id IN (1993) ) AND wp_posts.post_type IN ('post', 'trailer', 'feature', 'reviews') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
While I’m not sure what most of this means, I can see the taxonomy ID 268 does match Ben Affleck’s ID in the Actors taxonomy.
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesOk, I’ve tried the above code and still having some issues, but think I’ve managed to narrow it down to which line is causing problems!
When I got to /actors/ben-affleck now, it’s directing me to the 404 page. However, when I remove the
$query->is_tax = false;
line, I’m seeing the correct archive, though no posts are displaying.Just to make sure I’ve put everything in the right place, my current code looks like this…
if ( ! is_archive() && $query->is_main_query() ) return; // echo '<pre>', print_r( $query->query_vars, true ), '</pre>'; $tentative = $query->get('actors'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['actors']); } $tentative = $query->get('writers'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['writers']); } $tentative = $query->get('directors'); if ('' != $tentative ) { $person = $tentative; unset( $query->query_vars['directors']); } if ( ! isset( $person )) return; $query->is_tax = false; $taxquery = array( 'relation' => 'OR', array( 'taxonomy' => 'actors', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'writers', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'directors', 'field' => 'slug', 'terms' => $person, ), ); $query->set( 'tax_query', $taxquery ); } add_action('pre_get_posts','archive');
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesAh, I knew it would be a simple formatting thing somewhere! There are no posts showing yet but the echo is spitting out the following…
Array ( [actors] => ben-affleck [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) )
It looks like it isn’t pulling anything from the writers or directors taxonomies?
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesOk for some reason the forum won’t let my replies through with my current code but I have added the echo line between…
if ( ! is_archive() && $query->is_main_query() ) return
…and…
$query->set( 'post-type', array( 'post' , 'reviews' , 'trailer' , 'feature' ));
Which I think is the right place? At the moment it’s returning a parse error, unexpected echo, on all pages, with no other content coming through. Is it possible I’ve formatted it wrong and/or put it in the wrong place?
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesI added
if ( ! isset( $person )) return;
and now the other pages work correctly.However, as soon as I add the echo/print_r line, I just get this error on every page: Parse error: syntax error, unexpected ‘echo’ (T_ECHO) .
I think it’s in the right place, but just to confirm, my current function looks like this…
if ( ! is_archive() && $query->is_main_query() ) return echo '<pre>', print_r( $query->query_vars, true ), '</pre>'; $query->set( 'post-type', array( 'post' , 'reviews' , 'trailer' , 'feature' )); $tentative = $query->get('actors'); if ('' != $tentative ) $person = $tentative; $tentative = $query->get('writers'); if ('' != $tentative ) $person = $tentative; $tentative = $query->get('directors'); if ('' != $tentative ) $person = $tentative; if ( ! isset( $person )) return; $taxquery = array( 'relation' => 'OR', array( 'taxonomy' => 'actors', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'writers', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'directors', 'field' => 'slug', 'terms' => $person, ), ); $query->set( 'tax_query', $taxquery ); } add_action('pre_get_posts','archive');
Forum: Developing with WordPress
In reply to: Linking custom taxonomiesI think I’m getting there, though I’m not quite understanding this piece of code:
$tentative = $query->get('actors'); //correct taxonomy slug required here if ('' != $tentative ) $person = $tentative;
So I need to add it three times, for the three different taxonomies, but should I be giving each one separate IDs (instead of tentative and person, for example)?
What I have is currently this, although it isn’t returning posts, it is returning the error
Undefined variable: person
function archive($query) { if ( ! is_archive() && $query->is_main_query() ) return $query->set( 'post-type', array( 'post' , 'reviews' , 'trailer' , 'feature' )); $tentative = $query->get('actors'); if ('' != $tentative ) $person = $tentative; $tentative = $query->get('writers'); if ('' != $tentative ) $person = $tentative; $tentative = $query->get('directors'); if ('' != $tentative ) $person = $tentative; $taxquery = array( 'relation' => 'OR', array( 'taxonomy' => 'actors', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'writers', 'field' => 'slug', 'terms' => $person, ), array( 'taxonomy' => 'directors', 'field' => 'slug', 'terms' => $person, ), ); $query->set( 'tax_query', $taxquery ); } add_action('pre_get_posts','archive');
I’ve also noticed that I’m not seeing posts on any page now, just getting a ‘sorry, no posts available’ message, though presumably once this query is formatted correctly it should probably be fine!