0

Ok, the quick explanation:

I'm using a custom post type, 'lakes'. And, using custom taxonomies to organize them. They are: state, watertype and county.

I've created a 'page' for each state that displays all the listings using that state's name in the tags. This part works fine. What we want to do is also have some of the lakes set with a tag of 'featured' and display those in a div in the top portion of the page just above the regular results. There would only be perhaps 5 set as featured but the query could also set a limit as well.

Where i'm stuck is in how to create the query and results based upon the state page they are currently on. In other words, if they were on Alabama then i'd want the 5 featured listings in Alabama to display. If on Alaska, then the 5 featured from Alaska.

These would just display in a typical unordered list that I would format with CSS in the stylesheet. I just haven't gotten any query and loop code to work as it just wants to show the same 5 results no matter what state page they are on.

Ideas? Thoughts? Help? Thanks in advance to anyone and everyone. I AM researching the other similar posts as well but could not find the specifics about page-to-page issue.

2 Answers 2

1

If you don't need a "tag", I have seen "Featured" items be featured by some a post meta key. I always need a way to feature items, so yesterday I wrote a plugin that will add a metabox to any post type you'd like, with a simple checkbox. Also has columns and quickedit capabilities.

https://github.com/helgatheviking/Featured-Item-Metabox

Then I think you ought to be able to combine a Tax Query with a Meta Query. Pulling the taxonomy and term names of your current archive from the query like so:

$args = array(
    'posts_per_page' => 5,
    'tax_query' => array(
        array(
            'taxonomy' => get_query_var('taxonomy'),
            'field' => 'slug',
            'terms' => get_query_var('term')
        )
    ),
    'meta_query' => array(
           array(
               'key' => '_featured',
               'value' => 'yes'
           )
    )
);

$the_query = new WP_Query( $args );
0

First of all you didn't mention what taxonomies are your states? Secondly when you say "tag", again, what taxonomy do you mean? My suggestion you is to use meta key-value pair, to tag posts as featured and based on that query posts, however it is just my preference.

Anyway, if we assume you have states as additional taxonomies and have "tags" also as additional taxonomies, you just include the following code in the place where you want the addition list of "posts" to appear:

global $wp_query;
$args = array(
'posts_per_page' => 5,
'tax_query' => array(
    array(
        'taxonomy' => 'my-custom-tag',
        'field' => 'slug',
        'terms' => array('featured')
    )
)
);
$args = array_merge( $wp_query->query, $args );
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();
3
  • Sorry I wasn't clearer but thank you for the response! To clarify, 'lakes' is our custom post type and 'featured' is only a tag. The custom taxonomies are watertype (salt or fresh), state and county (to aid in searching and to avoid having actual categories for each one). So, our custom posts, 'lakes' are first segregated by state. When someone clicks on Arizona then all the 'lakes' with the 'arizona' taxonomy display both on the page in a list plus on a geo-locating map. It's on this page that we want a special section to show the lakes that are tagged as 'featured' but just for that state.
    – Simco
    Commented Jun 25, 2012 at 17:11
  • This could help, here's the link to the Alaska page and we have currently a widget that's attempting to display the featured lakes. Problem is it just displays any old lake, not ones marked featured for Alaska. fishingreports.com/new/state/alaska
    – Simco
    Commented Jun 25, 2012 at 17:20
  • I modified your code and tested it on a local installation of the site (XAMPP install) and no results displayed: [code]global $wp_query; $args = array( 'posts_per_page' => 5, 'tax_query' => array( array( 'taxonomy' => 'lakes', 'field' => 'slug', 'terms' => array('featured') ) ) ); $args = array_merge( $wp_query->query, $args ); $the_query = new WP_Query( $args ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; // Reset Post Data wp_reset_postdata(); [/code]
    – Simco
    Commented Jun 25, 2012 at 17:22

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.