Support » Plugin: Custom Post Type UI » Filter/multi-taxonomy query

  • Resolved mfebrey

    (@mfebrey)


    Hello! I’d like to add a filter option for Job Title (taxonomy) results. Would like to add Location (taxonomy) option to the results. Are there any code samples, documentation, tutorials? I’ve looked and can’t find anything. Thank you for much!

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Nothing that we have ready-made for this, since every implementation is going to be different. This is going to be all custom code that we don’t have involvement in, outside of just making sure the given taxonomy is registered and ready for use.

    Thread Starter mfebrey

    (@mfebrey)

    Can I pay(pal) you for support? Can’t find where to DM ya, happy to delete this.

    I have the code very close but can’t get past a hump.

     <?php 
    
    	$args = array(
    		    'relation' => 'AND',
        		array(
        			'taxonomy' => 'location',
        			'field'    => 'slug',
        			'terms'    => 'denver-co',
        		),
        		array(
        			'taxonomy' => 'job_titles',
        			'field'    => 'slug',
        			'terms'    => 'photographer',
        		),
    	);
    
        
    	// Custom query.
        $query = new WP_Query( $args );
        
        // Check that we have query results.
        if ( $query-> have_posts() ) {
         
            // Start looping over the query results.
            while ( $query->have_posts() ) {
         
                $query->the_post();
         
                echo "Success <br>";
         
            }
         
        } else {
            echo "FAIL";
        };
        
         
        // Restore original post data.
        wp_reset_postdata();
     
    ?>
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    What aspect of this is failing? Not finding posts?

    Based on the arguments made for the WP_Query call, you’d need a post to have both a location term of “denver-co” AND a job title of photographer” for it to match and be part of your results.

    Thread Starter mfebrey

    (@mfebrey)

    Something about the $query line is breaking the code. I can’t get any response from anything below it. Have removed my line by line echoes for ease of reading. But basically no echoes below the $query declaration get triggered.

    This is my first time messing with custom queries. Does the page need the WP default query to run first before adding the new query? What I’ve posted above is the only code on this particular page, “filters-test.php”. Fairly certain this breaks from the WP architecture and avoids the background includes.

    Does this help troubleshoot?

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    There’s a space in the if statement line between the -> and the have_posts() in your version above.

    if ( $query-> have_posts() ) {
    

    Should be

    if ( $query->have_posts() ) {
    

    No doesn’t need the default to make a new, secondary one.

    Thread Starter mfebrey

    (@mfebrey)

    Hey! Can’t even get the basic query to work.

    Made the code as simple as possible. This is the only code on the page:
    http://fshneco.com/v2/wp-content/themes/divi-child/filters-test.php.

    The code breaks at the Query declaration. Tried adding get header at the top and still no love. I’ll gladly post the final filter code once I’ve (we’ve) got this working!

     <?php 
    
        $args = array(
            'posts_per_page'   => -1,
            'post_type'        => 'any',
        );
    
        echo "args is ok <br>";
        
        // Custom query.
        $query = new WP_Query( $args );
        
        echo "query is ok <br>";
        
        // Check that we have query results.
        if ( $query->have_posts() ) {
         
            // Start looping over the query results.
            while ( $query->have_posts() ) {
         
                $query->the_post();
         
                echo "Success <br>";
         
            }
         
        } else {
            echo "FAIL";
        };
        
        echo "end of stuff"; 
         
        // Restore original post data.
        wp_reset_postdata();
     
    ?>
    • This reply was modified 3 years, 6 months ago by mfebrey.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not managing to recreate any errors with the code provided in the last reply. It found me 23 posts and iterated overall of them. Each time it gave the “success” message.

    Do you have any specific errors showing up somewhere like the error logs? That’d provide a lot better information for what’s actually happening.

    Thread Starter mfebrey

    (@mfebrey)

    No errors from the page itself but found this in the log:

    [autoindex:error]… Cannot serve directory /home/firestyleadmin/public_html/v2/wp-content/uploads/2019/05/: No matching DirectoryIndex (index.html.var….

    Could this be the result of my using the full path to the file and not a WP default directory structure?

    ie
    http://fshneco.com/v2/wp-content/themes/divi-child/filters-test.php

    I realize this is well outside your scope. Will pay for time.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you contact us at support@pluginize.com and we can take some of this away from public eye.

    Thanks.

    Thread Starter mfebrey

    (@mfebrey)

    FINAL WORKING CODE

    I hope this benefits someone else. Thanks a ton for the help Michael Beckwith!

    I am passing query vars via GET. This code was placed in my child theme folder and saved as filters.php. Then created a “Search” page in WordPress and assigned it the “Filters” template.

    Sample URL: https://www.fshneco.com/v2/search/?myloc=denver-co&myjob=model

     
    <?php 
    
    /* 
    Template Name: Filters
    */
        
    //grab variables from page url
    $loc = htmlspecialchars($_GET["myloc"]);
    $job = htmlspecialchars($_GET["myjob"]);
    	
    $args = array(
    	        
       //query only these custom post types
       'post_type' => array('collab', 'event'),
    	        
       //argument specifically for taxonomy
       'tax_query' => array(
    	            
       //AND because both conditions need to be met
       'relation' => 'AND',
                    
       //search tax terms based on GET vars from above
       array(
          'taxonomy' => 'location',
          'field'    => 'slug',
          'terms'    => $loc,
       ),
       array(
          'taxonomy' => 'job_titles',
          'field'    => 'slug',
          'terms'    => $job,
       ),
     ),
    );
    	
    //the actual query
    $query = new WP_Query( $args );
        
    //loop with results
    if ( $query->have_posts() ) {
         
       while ( $query->have_posts() ) {
         $query->the_post();
         $title = get_the_title();
         
         echo "Success= ".$title." <br>";
         
      }
         
    } else {
     echo "No results found.";
    };
        
    wp_reset_postdata();
     
    ?>
    
    • This reply was modified 3 years, 6 months ago by mfebrey.
    • This reply was modified 3 years, 6 months ago by mfebrey.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Woo, working things!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Filter/multi-taxonomy query’ is closed to new replies.