WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple categories on index page (3 posts)

  1. michaelgodfrey
    Member
    Posted 1 year ago #

    I'm struggling with get_posts, WP_Query, and query posts. I've read through The Loop, Custom Queries and about a bazillion other helper forums. I've even tried about a hundred examples from forums all over. I just can't seem to get what sounds like a simple task to work. I wish to show two categories on custom taxonomy index page. These are also linked to custom post types, which appear to have caused issues in older versions of WordPress.

    My scenario:

    Custom Taxonomy 1: Home Location - State - City
    Custom Taxonomy 2: Visiting Location - State - City

    I have template files isolating by location:
    taxonomy-home-location-new-york.php - example

    I have the visiting location set so that it auto changes to and from by date. I then want the visitor to show up in the home-location index page.

    As soon as I add a query of any type, all of the content on the page disappears. I only get the header and nav. I've stripped out all formatting and tried it with the simplest query I could find and it still doesn't work. Here is the code:

    <?php
    /**
     * The main template file.
     *
     *
     * @package Sunspot
     * @since Sunspot 1.0
     */
    
    define('WP_USE_THEMES', false); get_header(); ?>
    
    	<?php $my_query = new WP_Query('showposts=2&cat=7,28'); ?>
    
    		<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    		<?php the_title(); ?></a>
    		<?php the_content(); ?> //optional, can also be the_excerpt
    	<?php endwhile; ?>
    
    <?php get_template_part( 'primary_nav' ); ?>

    As a note: I've also tried it with and without define('WP_USE_THEMES', false); in the header's opening line.

    Any insight on what I'm doing wrong would be immensely appriciated.

  2. michaelgodfrey
    Member
    Posted 1 year ago #

    Update:

    I'm getting closer. The custom taxonomies and custom post types seemed to play a big factor.

    I can do a new WP_Query

    <?php $folio_loop = new WP_Query( array(
    	'post_type' => 'individuals',
    	'posts_per_page' => -1,
    	'home-location' => 'los-angeles',
    	'orderby' => 'menu_order') );
    ?>

    The above code will overide all individuals that live in new-york with los-angeles. So my query loop is working. Now my problem is how to properly array it. I've experimented and when I add a second value, I get every individual from every location in my query. The below example is one such attempt.

    <?php $folio_loop = new WP_Query( array(
    	'post_type' => 'individuals',
    	'posts_per_page' => -1,
    	array(
    		'home-location' => 'new-york',
    		'visiting-location' => 'visiting-new-york'),
    	'orderby' => 'menu_order') );
    ?>

    I would think that this would give me people living in the New York taxonomy and the people in the visiting taxonomy as well.

  3. michaelgodfrey
    Member
    Posted 1 year ago #

    Solved.
    Took a lot of trial and error, but here is what finally worked. The solution requires 'tax_query' and arrays within arrays.

    <?php $folio_loop = new WP_Query( array(
    	'post_type' => 'individuals',
    	'post_status' => 'publish',
    	'tax_query' => array(
    	'relation' => 'OR',
                      array(
                          	'taxonomy' => 'home-location',
    			'terms' => array('new-york','new-jersey','connecticut'),
                          	'field' => 'slug',
    		),
    		array(
                          	'taxonomy' => 'visiting-location',
    			'terms' => array('visiting-new-york'),
                          	'field' => 'slug',
    		),
    	),
    	'posts_per_page' => -1,
    	'orderby' => 'menu_order') );
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic