This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Tagged: ,

Split: Extending the custom query

  • Creator
    Topic
  • #17432
    Eleven Sites
    Participant

    Hi Jason,

    I’m having trouble getting the custom queries to work with my special case. I’ve created a Manufacturer post type. I’ve also created a Photos post type and a Videos post type that can be tagged with the manufacturer’s name. So, within my single manufacturer template, I want to pull in those other custom post types based on whether or not they were tagged with that manufacturer name.

    In my template, the following code will work.

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    		
     global $query_string;
    parse_str($query_string, $args);
         $args=array(
        'product-manufacturer' => $term->slug, 
        'post_type'         => 'video',
        'post_status'       => 'published',
        'order'           	=> 'ASC',
    	'order_by' 			=> 'title',
        'posts_per_page'   =>-1
        ); 
    $temp = $wp_query;
    	$wp_query = null;
    	$wp_query = new WP_Query();
    $wp_query -> query ($args);
    	while ($wp_query -> have_posts()): $wp_query ->the_post();
    	?><div id="<?php post_type_archive_title();?>"><h2><?php post_type_archive_title();?></h2><?php
        the_content();
        ?>
    	</div>
    	<?php
    	endwhile;
    	$wp_query = null;
    	$wp_query = $temp;

    But, I want to use a grid layout for the photos and videos. So, I tried this in my functions file:

    /**
     * Custom query for Photos on single Manufacturer pages
     */
    function custom_manufacturer_photos( $query, $args ){
    	if( $args['query'] == 'manufacturer_photos' ){
    	$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    		$query = array( 
    			'post_type' => 'photo',
    			'product-manufacturer' => $term->slug, 
    			'post_status' => 'publish',
    			'orderby' => 'title', 
    			'order' => 'ASC',
    			'posts_per_page' => -1,
    		);
    	}
    	return $query;
    }
    add_filter( 'themeblvd_posts_args', 'custom_manufacturer_photos', 10, 2 );

    And, this in my template:

    echo do_shortcode('[post_grid query="manufacturer_photos" link="false"]');

    But, getting the term by the post’s slug isn’t working. How do I achieve this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #17439
    Jason Bobich
    Keymaster

    You say you want to pull the posts “based on whether or not they were tagged with that manufacturer name” — What does this mean? How are the posts “tagged with that manufacturer name”? This suggests you should have a registered taxonomy, manufacturer?

    And it seems the basis of starting everything is hinging off this:

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

    But if you’re using this just in a random page, get_query_var( 'term' ) and get_query_var( 'taxonomy' ) are going to return nothing. In order for these query variables to exist, you’d need to be on a taxonomy archive, or have filtered into pre_get_posts, which I assume you’re not doing.

    #17440
    Jason Bobich
    Keymaster

    Is there a reason you need a “Manufacturer” post type? Shouldn’t this just be a taxonomy? You could still have nice Taxonomy archive page for each Manufacturer where you have information about it and list all Videos and Photos for the current Manufacturer.

    #17475
    Eleven Sites
    Participant

    This site has a lot going on and I’m still sorting out how to do all of the custom filtering…

    Basically, I have:

    Manufacturer post type
    -Custom Taxonomies:
    —Divisions
    —Product Categories
    —Product Keywords
    -Custom Fields:
    —Website URL
    —Quick Ship Options Available
    —Photos Available
    —Videos Available
    *Does not use WP default Tags or Categories

    Videos post type and Photos post type
    -Custom Taxonomies:
    —Divisions
    —Product Categories
    —Product Keywords
    —Product Manufacturer
    -Categories (Monthly Giveaways and Events)

    *Some of the photos and videos won’t be directly associated with a Manufacturer, and the ability to tag each photo/video via custom taxonomy was a requirement.

    The site’s main nav shows the company’s divisions. Each division has a Manufacturer page, where I have the manufacturers for that division only. And, I have (or am trying to create) a filter for the top of each Manufacturer page where site visitors can filter by 1) Open text search, 2) Product Category, 3) Product Keyword, and 4) Quick Ship Options Available. The template for the Manufacturer’s (list) layout has an icon indicating whether any Photos or Videos have been added for that Manufacturer.

    The (single) template for each Manufacturer, which I’m currently trying to figure out, must pull in everything associated with that manufacturer.

    The client also wants Photos and Videos sections with the same subpages: Manufacturers, Monthly Giveaways, and Events. The Manufacturer subpages of these sections will have their own filtering capabilities: 1) Open text search (that would search on the Product Manufacturer taxonomy), 2) Division, 3) Product Category, and 4) Product Keyword. The other two subpages in each section would be a straight forward listing queried by post type and category.

    I’ve been going around and around the best way to build this. So, I hope this all makes sense. I’d be happy to show you the site through a private message.

    Thanks,

    Lindsay

    #17483
    Jason Bobich
    Keymaster

    As far as the complexity of your post relationships and structure and how you’re going to write your custom queries are a bit outside of the realm of support that I can offer you. It sounds like maybe the overall organization could use some more thought, in terms of how this would best jive with WordPress.

    But it sounds like maybe you’re confusing specific instances of theme elements with dynamic template files.

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

    … This tells me that you’re not understanding quite the context in which you’re constructing these custom queries. In instances where you’re using the [post_grid] shortcode or Post Grid Page templates, even though you’re displaying posts of another post type, these are still being used on pages. So the above snippet I referenced would never be pulling the taxonomy and terms associated with your custom post type, and thus, your entire query becomes debunked. This is why you’re not getting any posts back.

    I think this is the big-picture idea you’re not understanding in using the “themeblvd_posts_args” filter. — If you were doing this concept for each “Manufacturer page”, you’d need to have to create a separate page for each manufacturer, apply the post grid page template to each of these pages, and set a unique key for each of these page and a corresponding queries.

    function my_custom_queries( $query, $args ){
    	
    	if ( $args['query'] == 'query_1' ) {
    		$query = array( 
    			// ...
    		);
    	} else if ( $args['query'] == 'query_2' ) {
    		$query = array( 
    			// ...
    		);
    	} else if ( $args['query'] == 'query_3' ) {
    		$query = array( 
    			// ...
    		);
    	}
    
    	// etc ...
    
    	return $query;
    }
    add_filter( 'themeblvd_posts_args', 'custom_manufacturer_photos', 10, 2 );

    And not to throw another huge wrench into an already confusing situation, but are you aware that you can display WordPress post archives in a grid with something called “grid mode” —

    http://dev.themeblvd.com/tutorial/grid-mode-adjusting-post-archives-to-display-in-a-grid/

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.