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: ,

Trouble with custom query in post grid page template

  • Creator
    Topic
  • #11545
    John
    Participant

    I am hoping to use the grid to display taxonomies for a custom post type using the code:

    function jma_classes_query( $query, $args ){
    	if( $args['query'] == 'jma_classes_query' ){die();
    		$query = array(
                'post_type' => 'jma_class',
                'orderby' => 'meta_value',
    			'sf_meta_key' => 'class_schedule/class_start_date',
                'order' => 'ASC',
    			'tax_query' => array(
    				array(
    				'taxonomy' => 'class_types',
                	'field' => 'slug',
                	'terms' => $args['this_tax']
    				)
    			),
                'paged' => $paged
            );
    	}
    	return $query;
    }
    add_filter( 'themeblvd_posts_args', 'jma_classes_query', 10, 2 );

    setting custom fields:
    query -> jma_classes_query
    and:
    this_tax -> fun-sewing-classes

    where fun-sewing-classes would be changed to reflect the slug of the tax to be displayed.
    Does this seem viable? right now it doesn’t die(); as I would expect it to so I’m not even getting step one right.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
    Replies
  • #11549
    Jason Bobich
    Keymaster
    #11550
    Jason Bobich
    Keymaster

    Hello Johnny,

    Can you tell me more about what you’re setting up in your WP admin for this? Is this a Post Grid element of a custom layout in the layout builder? And you’re sure you’re putting in “jma_classes_query” as the custom query string for the element being used?

    And where are you putting your custom code? Is this going in functions.php of the active child theme?

    #11552
    John
    Participant

    I’m using the post grid template and using:
    query and jma_classes_query
    as the name/ value pair in custom fields.
    Then hoping to use:
    this_tax and fun-sewing-classes
    as another name/ value pair in custom fields, to pass the taxonomy slug to the filter.
    thx

    #11553
    John
    Participant

    tried this also, with no success:

    function jma_classes_query( $query ){
    	global $post;
    	if( get_post_meta(get_the_ID(), 'query', true) == 'jma_classes_query' ){
    		$query = array(
                'post_type' => 'jma_class',
                'orderby' => 'meta_value',
    			'sf_meta_key' => 'class_schedule/class_start_date',
                'order' => 'ASC',
    			'tax_query' => array(
    				array(
    				'taxonomy' => 'class_types',
                	'field' => 'slug',
                	'terms' => get_post_meta(get_the_ID(), 'this_tax', true)
    				)
    			),
                'paged' => $paged
            );
    	}
    	return $query;
    }
    add_filter( 'themeblvd_posts_args', 'jma_classes_query', 10 );
    #11557
    Jason Bobich
    Keymaster

    With the Post Grid page template you’re working from a different area in how things are setup. The filter you want to use here is called themeblvd_template_grid_query — And FYI if you were working with the Post List page template it would be themeblvd_template_list_query.

    So, here’s an example of how you could get setup doing something similar with this filter. In this example, you’ve added a custom field with name “query” and value “jma_classes_query” —

    function my_template_grid_query( $query, $custom ) {
    	if ( $custom == 'jma_classes_query' ) {
    		// Setup custom $query...
    	}
    	return $query;
    }
    add_filter( 'themeblvd_template_grid_query', 'my_template_grid_query', 10, 2 );
    #11562
    John
    Participant

    This is what I came up with in the end (grabbed all the custom field at once instead of sending a second argument to the filter):

    function jma_classes_query( $query ){
    	$custom_fields = get_post_custom(get_the_ID());
    	if( $custom_fields['query'][0] == 'tax_classes_query' ){
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$query = array(
                'post_type' => 'jma_class',
                'orderby' => 'meta_value',
    			'sf_meta_key' => 'class_schedule/class_start_date',
                'order' => 'ASC',
    			'tax_query' => array(
    				array(
    				'taxonomy' => 'class_types',
                	'field' => 'slug',
                	'terms' => $custom_fields['tax_slug'][0]
    				)
    			),
    			'posts_per_page' => $custom_fields['tax_posts_per_page'][0],
                'paged' => $paged
            );
    	}
    	return $query;
    }
    add_filter( 'themeblvd_template_grid_query', 'jma_classes_query' );

    If I wanted to change the number of columns would that be possible?
    thanks,
    john

    #11577
    Jason Bobich
    Keymaster

    If I wanted to change the number of columns would that be possible?

    You can add a custom field “columns” with a value of 2-5 to your page with the post grid template.

    In your documentation there should be a list of all the custom fields you can use with these templates. Also, you might like this plugin that I posted recently:

    http://wordpress.org/plugins/theme-blvd-post-template-options/

    #11583
    John
    Participant

    Ok, for some reason I was under the impression that a custom query invalidated those options. Glad to see I was wrong.
    Just an additional note to share on the ‘themeblvd_posts_args’ filter. I came up with this solution for all the terms (slugs) in a taxonomy:

    function jma_machines_query(  $query, $args ){
    	$terms = get_terms('machine_type');
    	foreach ( $terms as $term ) {
    		$term_array[] = $term->slug;
    	}
    	if( in_array($args['query'], $term_array) ){
    		$query = array(
                'post_type' => 'jma_machine',
                'orderby' => 'menu_order',
                'order' => 'ASC',
                'posts_per_page' => -1,
    			'tax_query' => array(
    				array(
    				'taxonomy' => 'machine_type',
                	'field' => 'slug',
                	'terms' => $args['query']
    				)
    			),
            );
    	}
    	return $query;
    }
    add_filter( 'themeblvd_posts_args', 'jma_machines_query', 10, 2 );

    So entering any slug from the machine_type taxonomy triggers the query and acts as the ‘terms’ value for ‘tax_query’ array.
    thanks again,
    john

    #11586
    Jason Bobich
    Keymaster

    Just a note on the number of columns — By putting in your custom query, you are eliminating the framework’s posts_per_page for the columns, meaning if you have a 4×3 grid, you need to now manually designate 12 posts per page. However, there are a couple of other factors outside of the querying of posts, where this “columns” custom field is going to be applied to actually structure the columns. So essentially you need to do both.

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