Jump to content

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
https://forums.phpfreaks.com/topic/306560-help-with-widget-code-please/
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',      */
),
);
}

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

$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',
              
),
);
}

 

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', )

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',
 
  ),
);
}

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.

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

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,
 
),
    ),
 
  ),
);

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.