Jump to content

Help with widget code please


act5860

Recommended Posts

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

Link to comment
Share on other sites

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',      */
),
);
}
Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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.  :happy-04:    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',
              
),
);
}

 

Link to comment
Share on other sites

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', )
Link to comment
Share on other sites

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',
 
  ),
);
}
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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,
 
),
    ),
 
  ),
);
Link to comment
Share on other sites

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,)
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.