Skip Navigation

[Closed] List posts based on first letter of posts

This support ticket is created 4 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 8 replies, has 3 voices.

Last updated by Christian Cox 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1906895

I need to list all the posts that start with the first letter A.
But I don't see the possibility of doing that.
Can you help me with that?

#1907005

Hello, there's nothing exactly like this built-in to Views so it would require custom code using our PHP APIs. Another option is to create a custom taxonomy that includes one term for each letter A, B, C, etc., then apply the corresponding term to each post manually in wp-admin. Then you could create a View of posts filtered by the desired taxonomy term. The taxonomy approach is what I would recommend if you're not comfortable writing custom PHP code. If you're able to write PHP, I can show you how to use our Views Filter wpv_filter_query to modify the search query programmatically using your own custom code.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Let me know if you'd like to discuss either of these approaches in more detail, and I can give you some more guidance.

#1907391

Hi Christian,
i did it by myself as you required with taxonomy A, B, C ...
And that would work fine if I managed the website in the future.

But my client wants this to happen automatically, so they don’t have to think about so many things when creating a post.

So i prefer to do this with PHP.
I don't have a problem with PHP but im new in Toolset and how it's work.
This is my first project with Toolset so i need more time to learn where is things and how they works.

So yes please let's discuss second solution with query filters and PHP.

#1908139

But my client wants this to happen automatically, so they don’t have to think about so many things when creating a post. So i prefer to do this with PHP.
Okay sure, this makes sense. If you're new to Toolset / WordPress in general you may not be aware how to add custom PHP code to a site. Child themes and custom code snippets are two common approaches to adding custom code.
- Add the code to a child theme's functions.php file. Normally you should not add custom code directly to a theme, since updating the theme in the future will delete your changes. Instead, create and implement a child theme. Updates to the parent theme will not override the changes in your child theme. More information about child themes: https://developer.wordpress.org/themes/advanced-topics/child-themes/
- Create a new custom code snippet in Toolset > Settings > Custom Code tab, and add your PHP code here. Set the snippet to run everywhere and be sure to activate it when you are finished coding. These code snippets are stored as files on your server outside of the Toolset plugin directories, and will not be deleted or modified when Toolset plugins are updated or deactivated.

As I mentioned, the wpv_filter_query API documentation is available here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
The wpv_filter_query API provides a way to programmatically manipulate the WP_Query generated by a View. If you're not familiar with how WordPress queries work, you can find out more about WP_Query here:
https://developer.wordpress.org/reference/classes/wp_query/
This ticket includes an example of a wpv_filter_query customization similar to what you're asking about: https://toolset.com/forums/topic/filtering-artist-by-selecting-the-first-letter-of-the-title/#post-602852
You can see the example includes a SQL query directly on the database to search by the first character of the post title:

$postids = $wpdb->get_col($wpdb->prepare("SELECT      ID
 FROM        $wpdb->posts
WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
AND post_type = '".$query['post_type'][0]."'
AND post_status = 'publish'
ORDER BY    $wpdb->posts.post_title",$first_char)); 

I'm not a SQL expert, so I'm not really the best source of information about querying the database directly like this, but you can get the idea from this example. The $postids returned by this SQL query are then used to set the original $query's 'post__in' filter argument. More about post query filters here: https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
The wpv_filter_query filter passes the manipulated post__in filter parameter along to the original query, the query continues, and results are generated. That's the general idea. If you need more information about how the wpv_filter_query filter works in the general request lifecycle, please let me know.

#1908555

Thank you Christian,
it is great explanation.

Anyhow i don't know where to put this shortcode in my View:

[wpv-layout-start]
    [wpv-items-found]
    <a href="[current_url]">All</a>
    <!-- wpv-loop-start -->
        <wpv-loop>
            <a href="?wpvalphabet=[wpv-taxonomy-title]">[wpv-taxonomy-title]</a>
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
    [/wpv-no-items-found]
[wpv-layout-end]

My code looks more like this if i change from Visual to Code editor:

<!-- wp:toolset-views/view-editor {"reduxStoreId":"views-editor-1610929071839","viewId":1832,"viewSlug":"alphabet","previewId":1833,"focused":false,"insertExisting":"0","wizardDone":true,"wizardStep":3} -->
<div class="wp-block-toolset-views-view-editor wpv-gutenberg-view-wrapper-1832"><!-- wp:toolset-views/view-layout-block -->
<!-- wp:toolset-views/view-template-block {"storeId":"views-editor-1610927733906","style":{"cssClasses":[]}} -->
<div class="wp-block-toolset-views-view-template-block wpv-block-loop-item php-to-be-replaced-with-shortcode" data-toolset-views-view-template-block="1"><!-- wp:toolset-blocks/heading {"dynamic":{"content":{"isActive":true,"provider":"__current_post","source":"post-title-with-link"}}} -->
[tb-dynamic-container provider='__current_post' source='post-title-with-link' field='']<h2 class="tb-heading" data-toolset-blocks-heading="1" data-last-update="1.4">[tb-dynamic provider='__current_post' post='current' source='post-title-with-link' force-string='first' ]</h2>[/tb-dynamic-container]
<!-- /wp:toolset-blocks/heading --></div>
<!-- /wp:toolset-views/view-template-block -->
<!-- /wp:toolset-views/view-layout-block --></div>
<!-- /wp:toolset-views/view-editor -->

I can see on forum that people using this code but i really don't know where to place it.

#1909837

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Christian is currently on holidays today but will be back tomorrow to continue assisting with this one.

However looking at the issue here this code below is actually from our Classic Editor for views.

[wpv-layout-start]
    [wpv-items-found]
    <a href="[current_url]">All</a>
    <!-- wpv-loop-start -->
        <wpv-loop>
            <a href="?wpvalphabet=[wpv-taxonomy-title]">[wpv-taxonomy-title]</a>
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
    [/wpv-no-items-found]
[wpv-layout-end]

The second code you've sent is from our Blocks interface. In order to have the views interface you will need to enable the classic editor by going to Toolset -> Settings and scrolling to editing experience. From there you should see 3 options, select the 3rd option.

Once the page is saved you can refresh and you will now see the option Toolset->Views .

Thanks,
Shane

#1910801

Hi, just checking in to see if you were able to resolve this one or if you needed some additional guidance. Let me know if you need anything else from support.

#1912033

Hi guys,
i have some other things to do right now and will test it asap and let you know results.

#1912197

No problem, I'll stand by for your update.

The topic ‘[Closed] List posts based on first letter of posts’ is closed to new replies.