How to get the slug using custom query?

Support MB Views How to get the slug using custom query?Resolved

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #33912
    EddyPiVEddyPiV
    Participant

    Hi,

    How do I get the slug in custom query? I know to get the permalink, but it's the slug that I need.

    #33913
    EddyPiVEddyPiV
    Participant

    Actually, my question is more complex.

    Clients can buy a series of recipes. They become gradually available to the buyer.
    There is a standard set of recipes, with permalinks .../cpt/recipe-01, -02, -03 etc.
    At preset interval the next recipe of the standard set is released to the buyer, by copying the standard recipe to .../cpt/<username>-recipe-01, -02 etc.
    This is all working fine.

    I want to show an overview of the released recipes to the buyer.
    For several reasons I want to do that with the standard set of recipes as starting point, by looking if the permalink .../cpt/<username>-recipe-01, 02 etc. exist.

    Therefore I need the following steps:
    - determine the slug of the standard recipe (my starting question)
    - find out for each recipe if a post for the buyer exists.

    Next to my starting question on the slug, I'm having difficulty with
    - getting the username
    - determining if a post for the buyer exists.

    I hope you can help me.

    #33934
    Long NguyenLong Nguyen
    Moderator

    Hi,

    To get the post slug, you can use the property post_name
    {{ post.post_name }}
    Refer to the documentation https://docs.metabox.io/extensions/mb-views/#custom-query

    Then you can update the post slug with author's name after saving the post by using the WP function wp_update_post(). Refer to this topic
    https://support.metabox.io/topic/append-a-related-posts-title-to-the-title-of-created-post/

    #34055
    EddyPiVEddyPiV
    Participant

    Hi Long,

    Thanks for helping me each time a step further.

    I find it hard to find all answers myself in WordPress codex and in other sources, although I'm spending hours on it. And yet I have to come to you with what I believe are so basic questions.

    I managed to get everything in order. Only part left ìs the condition if a recipe for the buyer exists.
    I'll share my code. It results in a critical error on the website.
    First I read the standard recipes from 1 package. Then I'll check which recipes of the package exist for the current user, and I show the details. (slug for the recipe for the buyer = <userid>>-recipe-01 etc.)

    '''
    {% set args = { post_type: 'xtra', include: [ 281, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292 ], order: 'asc' } %}
    {% set posts = mb.get_posts( args ) %}
    {% set user_ID = mb.get_current_user_id() %}
    {% set abs_path = 'https://example.com/xtra/' %}
    {% set rel_path = 'xtra/' %}

    <div class="xtra_container">
    {% for post in posts %}
    {% if ( $page = mb.get_page_by_path( {{ rel_path }}{{ user_ID }}-{{ post.post_name }} ) ) %}
    {
    <div class="xtra_item">
    {% set post_link = mb.get_permalink( post.ID ) %}
    <h3>{{ post.post_title }} </h3>
    [su_row][su_column size="1/2" center="no" class=""]{{ mb.get_the_post_thumbnail( post.ID, 'thumbnail' ) }} [/su_column]
    [su_column size="1/2" center="no" class=""][icon name="calendar-check" prefix="fas"] {{ post.post_modified | date( 'F j, Y' ) }} <br> [icon name="comment" prefix="fas"] {{ post.comment_count }} [/su_column][/su_row]
    </div>
    }
    {% endif %}
    {% endfor %}
    </div>
    '''

    Can you help me to get this corrected?

    #34077
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I think it would be easier if you use the WP function get_the_permalink() to get the full permalink (string) of the post. Then use the PHP function strpos() to check if a string is available in the permalink string. For example:

    {% set user_string = 'username' %}
    {% set args = { post_type: 'post', order: 'asc' } %}
    {% set posts = mb.get_posts( args ) %}
    {% for post in posts %}
        {% set permalink = mb.get_the_permalink( post.ID ) %}
        {% set pos = mb.strpos( permalink, user_string ) %}
        {% if pos %}
            {{ permalink }} <br>
        {% endif %}
    {% endfor %}

    Read more on the documentation
    https://developer.wordpress.org/reference/functions/get_permalink/
    https://www.php.net/manual/en/function.strpos.php

    #34087
    EddyPiVEddyPiV
    Participant

    I understand what you're doing. But I'm concerned that over time, when both the number of recipes and the number of sales are growing, the performance will become an issue. And with an installed base by then, I'm afraid that getting that solved can become quite a headache.

    Therefore I'm looking into a view per set of recipes, I'm sort of hardcoded reading the posts of the set (in this case 11 posts) and subsequently reading/checking which recipes of this set exist for the current user. I.e. getting the post based on the permalink with the username in it.
    This way the number of posts to be read is limited to 2x number of recipes in the set.

    But my question then is how to read the post by permalink, and how to do the proper coding for the condition if the post exists.

    Hope you can help me with that.

    Thanks a lot!

    #34108
    Long NguyenLong Nguyen
    Moderator

    Hi,

    I'm not sure about the benefit when you add the username/user ID to the URL of the post. You can try to create a relationship between posts and users then set up the connection as well. For the set of posts, you can use the custom taxonomy to group the posts that are connected to a user. I think it would be easier to implement the code.

    Another way, you can create a post on Facebook to ask for another solution for your case.
    https://www.facebook.com/groups/metaboxusers

    #34211
    EddyPiVEddyPiV
    Participant

    Thanks Long, I found the solution through the FB group. Can you close this threat?

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.