• I have a hierarchical custom taxonomy of ‘Location’ which allows my client to specify the country > county > city (UK based) of a custom post type.

    My client, however, needs the option to specify some posts as being in *every* city. So no matter what the WordPress query is for ‘location’, one of these posts should be within the results.

    I’m wondering, is the only way to achieve this by my client manually applying the post to every single city? Or is there some kind of workaround I could do to make it show within the results? I’m thinking maybe with a custom field.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter 000000000

    (@pealo86)

    I’m actually thinking I could set up a custom field called something like ‘location-all’ with a value of whatever (let’s say ‘1’ in this case) that could gather those that are applied for every location.

    However, I’m not sure how to add this into a wordpress query. In my query I would need to add something like the following:

    &location-all=1

    But then the query would ONLY pull in those posts that have this value. I need it to pull these posts in ASWELL as those that match the other criteria in the query.

    I’m thinking maybe I need to set up two separate queries and merge the results together, but I have no idea how to do that!

    I don’t think I would bother with a custom field. I would try adding ‘everywhere’ to the Location taxonomy and then any company that needs to be listed in all locations would just have ‘everywhere’ selected.

    Then do a single query for multiple taxonomy terms, something like..

    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'location',
    			'field' => 'slug',
    			'terms' => array( 'liverpool', 'anywhere' ),
    			'operator' => 'IN'
    		)
    	)
    );
    $query = new WP_Query( $args );

    That should list items with either liverpool or anywhere as the taxonomy term.

    Thread Starter 000000000

    (@pealo86)

    Thanks again! Just wondering though, would there be a way to incorporate this in query_posts instead?

    As my query currently looks like this and would be a little awkward to transform into WP_Query instead:

    <?php $query = array(
    	'meta_key' => 'premium',
    	'orderby' => 'meta_value title',
    	'order' => 'ASC',
    	'posts_per_page' => -1,
    	'paged' => $paged
    ); ?>
    <?php if($_GET['Partner']) : ?>
    	<?php $query ['post_type'] = $_GET['Partner']; ?>
    <?php else : ?>
    	<?php $query ['post_type'] = 'recruiter-partner'; ?>
    <?php endif; ?>
    <?php if($_GET['County']) : ?>
    	<?php $query ['location'] = $_GET['County']; ?>
    <?php elseif($_GET['Region']) : ?>
    	<?php $query ['location'] = $_GET['Region']; ?>
    <?php elseif($_GET['Country']) : ?>
    	<?php $query ['location'] = $_GET['Country']; ?>
    <?php endif; ?>

    I *think* you can just add that query stuff into query_posts(). So adding this to the bottom of your current code:

    <?php $query = array(
    	'meta_key' => 'premium',
    	'orderby' => 'meta_value title',
    	'order' => 'ASC',
    	'posts_per_page' => -1,
    	'paged' => $paged
    ); ?>
    <?php if($_GET['Partner']) : ?>
    	<?php $query ['post_type'] = $_GET['Partner']; ?>
    <?php else : ?>
    	<?php $query ['post_type'] = 'recruiter-partner'; ?>
    <?php endif; ?>
    <?php if($_GET['County']) : ?>
    	<?php $location = $_GET['County']; ?>
    <?php elseif($_GET['Region']) : ?>
    	<?php $location = $_GET['Region']; ?>
    <?php elseif($_GET['Country']) : ?>
    	<?php $location = $_GET['Country']; ?>
    <?php endif; ?>
    <?php
    $query['tax_query'] => array(
    		array(
    			'taxonomy' => 'location',
    			'field' => 'slug',
    			'terms' => array( $location, 'anywhere' ),
    			'operator' => 'IN'
    		)
    	)
    );
    ?>

    Something along those lines should display all items in that location as well as all the ones with the location set as ‘anywhere’.

    Thread Starter 000000000

    (@pealo86)

    Hmmm can’t quite get this to work unfortunately? Not sure where that $location variable is coming from?

    Thread Starter 000000000

    (@pealo86)

    Ahhh wait ignore that… I’m on the right track now.

    Thanks again ??

    My explanation wasn’t the best. I started out saying ‘add this to the bottom’ but then added the code and realised some more stuff would need to change to get the location so tried to make some more changes to your code and probably confused things a little.

    Got there in the end.

    Thread Starter 000000000

    (@pealo86)

    Hmmm everything seems to be working quite smoothly, except with one small issue!

    I have the following:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    But a problem crops up that affects the ordering of premium listings:
    https://www.ads-software.com/support/topic/order-a-query-by-meta_value-and-then-by-title?replies=3

    If you look here, you can see that the premium listing (which is also marked as anywhere) is not at the top as it should be
    https://2.gp/p2hv

    I am using this to order the posts:
    'orderby' => 'meta_value title',

    However the posts pulled in set as ’01-anywhere’ are not being put at the top when they are marked as premium also?

    Thread Starter 000000000

    (@pealo86)

    Aghhh, here is the code again:
    https://pastebin.com/dUagCKM7

    What sort of data is ‘premium’?

    If it’s a number you might want to try using meta_value_num for the orderby rather than meta_value.

    Thread Starter 000000000

    (@pealo86)

    It has a value of either ’01. Yes’ or ’02. No’

    Tried with ‘meta_value_num’ but still no luck ??

    It’s wierd because it orders them properly on this page:
    https://2.gp/p2hw

    But gets the order completely wrong on the other one!

    Thread Starter 000000000

    (@pealo86)

    Managed to solve it in the end, I noticed the recruiter partner posts worked fine, but the supplier partners just wouldn’t order correctly!

    So I just removed the posts and added new ones, problem solved! Bit of a weird one though.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Applying a post to *every* category / taxonomy’ is closed to new replies.