act5860 Posted February 17, 2018 Share Posted February 17, 2018 On my WordPress webpage I have a category widget that is presented as a dropdown. I've been trying to modify the widget-items-category.php to restrict the listing to those where count > 10. I've marked the section that I think is where the modification needs to be made but none of the changes I've made have helped. Can someone PLEASE suggest how to accomplish this? Thanks in advance. widget-items-category.php Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/ Share on other sites More sharing options...
requinix Posted February 17, 2018 Share Posted February 17, 2018 It'll be a lot easier for us if you put the relevant part of the code directly into your post. So we don't have to read through 13KB of stuff to find what you already know we need to look for. Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556484 Share on other sites More sharing options...
act5860 Posted February 17, 2018 Author Share Posted February 17, 2018 This is the section of code that I believe needs to be changed: /* I believe this is where the dropdown category box gets populated */ global $post; $not_in = array(); if( is_singular() ) { $not_in []= $post->ID; } $not_in = array_merge ( $not_in, get_option( 'sticky_posts' ) ); $args = array(); $tax = 'category'; if( '0' != $instance['category'] ) { $args = array( array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', /* 'field' => 'count', this modification did not work 'operator' > '10', */ ), ); } Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556485 Share on other sites More sharing options...
requinix Posted February 17, 2018 Share Posted February 17, 2018 $args = array( array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', /* 'field' => 'count', this modification did not work 'operator' > '10', */ ), );PHP arrays cannot have multiple items with the same key. You need to look into WP_Query to see how does searches and whether it supports multiple taxonomy searches at once. Fortunately it does. Take a look at how it says your query array needs to be set up in order to have one condition by the term_id and the new condition you're adding by the count. Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556487 Share on other sites More sharing options...
act5860 Posted February 17, 2018 Author Share Posted February 17, 2018 $args = array( array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', /* 'field' => 'count', this modification did not work 'operator' > '10', */ ), ); PHP arrays cannot have multiple items with the same key. You need to look into WP_Query to see how does searches and whether it supports multiple taxonomy searches at once. Fortunately it does. Take a look at how it says your query array needs to be set up in order to have one condition by the term_id and the new condition you're adding by the count. Thanks for that. Does the following look correct? global $post; $not_in = array(); if( is_singular() ) { $not_in []= $post->ID; } $not_in = array_merge ( $not_in, get_option( 'sticky_posts' ) ); $args = array(); $tax = 'category'; if( '0' != $instance['category'] ) { $args = array( array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', ), array( 'relation' => 'AND', array( 'taxonomy' => 'category', 'field' => 'count', 'operator' => '> 10', ), ); } Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556497 Share on other sites More sharing options...
requinix Posted February 17, 2018 Share Posted February 17, 2018 Look more closely at the "Multiple Taxonomy Handling" example: you have array(conditions) array(AND array(conditions) )but it needs to be array(AND array(conditions) array(conditions) ) Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556504 Share on other sites More sharing options...
act5860 Posted February 17, 2018 Author Share Posted February 17, 2018 OK, I'm stumped. I have no idea what to nest in the arrays. I think what I have in my array should probably be in the second array but can't figure out what should be in the first array : array( 'relation' => 'AND', array( can't figure out what goes here ) array( 'taxonomy' => 'category', 'field' => 'count', 'operator' => '> 10', ) Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556506 Share on other sites More sharing options...
requinix Posted February 17, 2018 Share Posted February 17, 2018 The first set of conditions. Because you're adding the new one about the count but need to keep the old one about the terms. Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556507 Share on other sites More sharing options...
act5860 Posted February 17, 2018 Author Share Posted February 17, 2018 So, like this? global $post; $not_in = array(); if( is_singular() ) { $not_in []= $post->ID; } $not_in = array_merge ( $not_in, get_option( 'sticky_posts' ) ); $args = array(); $tax = 'category'; if( '0' != $instance['category'] ) { $args = array( array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', ), array( 'relation' => 'AND', array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', ), array( 'taxonomy' => 'category', 'field' => 'count', 'operator' => '> 10', ), ); } Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556508 Share on other sites More sharing options...
requinix Posted February 17, 2018 Share Posted February 17, 2018 Yeah, but now you have them in there twice. The entire $args is the array containing the AND and the two conditions. $args = array( "relation" => "AND", array(term condition), array(count condition) );In other words, it's the same structure that it originally started as - an array containing the array with the term conditions - plus the new array for the count condition, and to tell WP what you want to do with two you also put the "relation" in there. Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556509 Share on other sites More sharing options...
act5860 Posted February 18, 2018 Author Share Posted February 18, 2018 I'm sorry, you've lost me. Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556534 Share on other sites More sharing options...
requinix Posted February 18, 2018 Share Posted February 18, 2018 $args is an array. Obviously. It contains the information needed by WP in order to do a search. That information is basically more arrays. You started with $args being an array containing only one sub-array of information for a search: the terms. Now you want to add another sub-array with information for the search: the count. So $args will be the same array as before but with a second sub-array added to it. But now that $args has two arrays in it, WP doesn't know what you want to do with both. Do you want both conditions to apply? Either condition? It doesn't know. So in $args you add the "relation" key that tells WP you want to "AND" the two conditions. That way when it builds the SQL query it will take the two sub-arrays, build the conditions each one represents, and join them together with an AND. Does that make sense? Can you see what $args is supposed to look like yet? Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556536 Share on other sites More sharing options...
act5860 Posted February 18, 2018 Author Share Posted February 18, 2018 Is this what you're talking about? global $post; $not_in = array(); if( is_singular() ) { $not_in []= $post->ID; } $not_in = array_merge ( $not_in, get_option( 'sticky_posts' ) ); $args = array(); $tax = 'category'; $int = < 10 if( '0' != $instance['category'] ) { $args = array( array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN', array( 'relation' => 'AND', array( 'taxonomy' => $tax, 'field' => 'count', 'operator' => $int, ), ), ), ); Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556537 Share on other sites More sharing options...
act5860 Posted February 18, 2018 Author Share Posted February 18, 2018 Or are you saying that after the first $args array I need to do this: $args = $args + array( 'relation' => 'AND',) $args = $args + array( 'taxonomy' => $tax, 'field' => 'count', 'operator' => $int,) Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556538 Share on other sites More sharing options...
requinix Posted February 19, 2018 Share Posted February 19, 2018 Neither. The first one is what I've already told you isn't correct, and the second isn't really that much better. I tried. $args = array( "relation" => "AND", array( 'taxonomy' => $tax, 'terms' => array( $instance['category'] ), 'field' => 'term_id', 'operator' => 'IN' ), array( 'taxonomy' => $tax, 'field' => 'count', 'operator' => '> 10' ) );That's assuming you have the right query condition. I don't know enough to tell you if you do, but you'll find out soon enough. Compare that with the version from the documentation, which I'm going to copy and paste right in here so you don't have to click a link to find it. $args = array( 'post_type' => 'post', 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'movie_genre', 'field' => 'slug', 'terms' => array( 'action', 'comedy' ), ), array( 'taxonomy' => 'actor', 'field' => 'term_id', 'terms' => array( 103, 115, 206 ), 'operator' => 'NOT IN', ), ), );While your code also uses a variable called "args", what it's actually doing (IIRC) is creating that "tax_query" portion. Just that. So ignore the rest. Doesn't it look very similar? Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556539 Share on other sites More sharing options...
act5860 Posted February 19, 2018 Author Share Posted February 19, 2018 It didn't work, couldn't even open it in WordPress. Thank goodness that I saved the original under a different filename. Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556540 Share on other sites More sharing options...
act5860 Posted February 19, 2018 Author Share Posted February 19, 2018 Question for you: count is a numeric field and I've been passing the operator as a string. Could that be why I couldn't get it to work? Quote Link to comment https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/#findComment-1556541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.