Skip Navigation

[Resolved] View controlled via shortcode attribute to restrict posts starts with between two letters

This thread is resolved. Here is a description of the problem and solution.

Problem:
View controlled via shortcode attribute to restrict posts starts with between two letters

Solution:
You will require to use the View/Block filter hook "wpv_filter_query" and adjust the view's query to add character range.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/view-query-controlled-via-shortcode/#post-1988813

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by katjaS-3 3 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1986747

Hi,

my directory page is a Blog for Book Reviews.
Therefore I created the Custom Post Type "Reviews" contain the custom fields group "Book author" with the field "Lastname".

I created a page that should show the list of all book authors. To keep it handy I wann put it into a accordion. So that a have a section A-B-C and D-E-F and so on.

If I am using the "between" criteria and link it to a beginning and ending statement/variable it works in generell OK.
In the accordion I state: [wpv-view name="View_Buchautoren" begin="A%" end="D%"] for A-B-C
If I limit to "C%" it will not show author's lastnames starting with "C" whyever.
Question 1: --> How to solve it for Y-Z?

Question 2:--> How am I getting a DISTINCT statement into the query?

THX in advance

Frank

#1986799

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean that when you set your view to start with A and end with C, you expect to see the posts with last name start from A,B and C but in that case the posts that starts C in lastname is not showing?

In the accordion I state: [wpv-view name="View_Buchautoren" begin="A%" end="D%"] for A-B-C
==>
If you are using above method that helps you to display posts starts with A and C, its correct.

As you can see with the following link:
- hidden link

And try to run the query:

SELECT * FROM Customers
WHERE CustomerName Between "A%" and "D%"

It shows the results where CustomerName starts with A,B and C. So, the method you are using is correct I believe and how it suppose to work.

If you want to solve it, then I think we need to add hook to modify the query and instead of using between we have to use the like operator with or clause.

Question 2:--> How am I getting a DISTINCT statement into the query?
--
I'm not sure what you are saying here, the queries will be automatically generated by views so you should not worry about.

#1986803

Hello Minesh,

THX for the fast answer.

Yes, I would like to switch to "LIKE" with "or" but unfortunately I cannot find a documentation that tells me the right syntax (for the shortcode) and all my attempts failed. Question 1 (again) Can your please tell be how the short code must look like?

Question 2:
Distinct means, that of 2 or more posts have the same author (lastname) I only wanna have the name appear only once.

URS
Frank

#1986827

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes, I would like to switch to "LIKE" with "or" but unfortunately I cannot find a documentation that tells me the right syntax (for the shortcode) and all my attempts failed. Question 1 (again) Can your please tell be how the short code must look like?
==>
Well - for that we will require to use the hook wpv_filter_query.

Can you please share problem URL where you added your view and temporary admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

Question 2:
Distinct means, that of 2 or more posts have the same author (lastname) I only wanna have the name appear only once.
==>
Ahh ok.

#1986889

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I try to login to wp-admin, it shows the following error when I try to submit username and password:

Unbekannter Benutzername. Überprüfe ihn noch einmal oder versuche es mit deiner E-Mail-Adresse.

Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#1988561

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I again tried with both old and new passwords you shared with me but no luck, the supplied admin access details is not working at this end.

Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#1988813

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

function func_starts_with_character( $query_args ,$view_settings, $view_id ) {
    global $post;
     
    if ( $view_id == 471 ) {
        
         $start = do_shortcode("[wpv-attribute name='anfang']");
         $end = do_shortcode("[wpv-attribute name='ende']");
      
      
      $range = range($start,$end);
      $relation = array('relation'=>"OR");	  
      $args = array();
      foreach($range as $k=>$v):
      	       $args[]  =   array(
                    'key' => 'wpcf-nachname',
                    'value' => '^'.$v,
                    'type' => 'CHAR',
                    'compare' => 'REGEXP'
                 );

      endforeach;
        $args = array(array_merge($relation,$args));
      
      $query_args['meta_query'] = array_merge($query_args['meta_query'],$args);
         
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_starts_with_character', 10, 3);

Now, you should adjust your shortcode, for instance, if you want to display posts starts with A, B and C, you should adjust your view's shortcode as:

[wpv-view name="View_Buchautoren" anfang="A%" ende="C%"]
#1989691

Hi Minesh,
I like the code a lot. THX for that. Really great help.

Last little question. Any idea how I could add something similar like the SQL statement "SELECT DISTINCT" to the meta_query?

THX
Frank

#1989999

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no such feature available but you may try to use the WordPress hook: posts_groupby

For example:

function  query_group_by_filter($groupby){
       global $wpdb;

      return $wpdb->postmeta . '.meta_value ';
}

And I've added the filter hook within the view's hook: wpv_filter_query in "Custom Code" section.
=> hidden link

add_filter('posts_groupby', 'query_group_by_filter');	         
#1990319

Minesh,

perfect! Very cool.

Last litte question. I am usually using the iThemes security plugin. In my sandbox have deactivated the Plugin to your convenience. Unfortunately I believe that your custom code is inhibited by iThemes. Any idea by chance witch section this could be?

URS
Frank

#1990323

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

We can entertain only one question per ticket. This will help other users searching on the forum. Please kindly open a new ticket with every new question you may have. Thank you for understanding.

I'm not sure what you mean exactly, do you mean that itheme security has remove the custom code I added?

#1998027

My issue is resolved now due to best code snippet ever! Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.