• Resolved andynick

    (@andynick)


    Is it possible to change the order in which posts are listed in a given category – such as by using a number in a custom field?
    Thanks, Andy

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 19 total)
  • Hey @andynick!

    In general, you can change the default sorting of your post archives (and filter them) on any theme by using the pre_get_posts function. This custom pre_get_posts function can be included in your theme’s functions.php file or be added using a plugin such as Code Snippets.

    If you are not confortable coding this, there are also a few plugins that will allow you to change the way posts are sorted.

    using a number in a custom field

    In this scenario, you could check for the post meta before you run the pre_get_posts function and set different conditions inside that function depending on the content of said post meta.

    Would this make sense? Were you referring to something this?

    Thread Starter andynick

    (@andynick)

    Thanks for your help @mrfoxtalbot.
    I happen to have the Code Snippets plugin, but unfortunately, I don’t understand how to get the pre_get_posts thing to work.

    One reason for asking the question is that I’m trying to reduce the number of plugins on the site, and the current plugin (which displays posts in the order dictated by a number which I put in a custom field) is quite costly.

    I’m really sorry but I don’t know what you mean by “check for the post meta before you run the pre_get_posts function”…

    At the moment, my posts display in the reverse order of their creation date (the default). Which doesn’t always make any sense.

    I was hoping to avoid amending the creation dates – there are a lot of posts!

    Alvaro Gómez

    (@mrfoxtalbot)

    Hi @andynick!

    I’m really sorry but I don’t know what you mean by “check for the post meta before you run the pre_get_posts function”

    There are a few general examples of how you can use pre_get_posts here.

    When you write that pre_get_posts function, you can check if a given custom field (custom field ≅ post meta) has any content and what its content. You can then set different filters/orders based on this value.

    That being said, I have the feeling I do not fully understand the situation. Could you please share more details on how this custom field works and maybe a link to an example?

    Also, if you can, please take screenshots of what you are seeing as well, you can upload them to a site like https://snipboard.io/ and share the link.

    Thank you!

    Thread Starter andynick

    (@andynick)

    Thanks again @mrfoxtalbot, you’ve been really helpful, but I looked at the examples of pre_get_posts and I can’t even make out where I should add any info to configure them. It’s just complex letters, numbers and symbols to me!

    I’m not bad at HTML or CSS, but anything more advanced…

    With the plugin I’m using, I just put a number (eg. 01, 02, 03 etc) into a custom field (eg view-order) of each post in a given category.

    I then configure the plugin to sort the posts in that category, by the numbers in the custom fields ‘view-order’ and the plugin sorts the posts into that order.

    Ideally, I’d like to use the Code Snippets plugin to do something similar and I’m sure the examples on your link are close, but I can’t find one (or how to modify one) to list posts in (my) custom-view order.

    I’m happy to provide screen shots – but of what??

    This is a typical page sorted by the plugin I currently use.

    Lastly, are you a photographer by any chance??

    Alvaro Gómez

    (@mrfoxtalbot)

    Thank you for the additional details, Andy. I think I have a clearer idea of what you need now.

    I think you can do away with that plugin and go with the “vanilla” custom fields. The block editor does not show custom fields by default, but you can activate them by heading to the three dots on the top right and navigating to Preferences > Panels.

    Once you do this, you will see the custom fields box appear at the bottom of the editor, where you can add a new “meta” key to set this order.

    Here’s a video illustrating the steps I explained above.

    The next step will be to modify the order of your posts. Below is an example of a short snippet you can add using Code Snippets that will re-order your posts based on the value of this custom field:

    add_action( 'pre_get_posts', 'andys_custom_post_order' );
    function andys_custom_post_order( $query ) {
        // re-order front-end "faq" category archives using the value of the "my_post_order" post meta
        if( is_category( $category = 'faq') && !is_admin() && $query->is_main_query() ) {
            $query->set( 'posts_per_page','10' );
            $query->set( 'orderby','meta_value' );
            $query->set( 'meta_key','my_post_order' );
            $query->set( 'order','ASC' );
        }
    }

    The code above will ONLY change the order on the “faq” archives. This can be tweaked by changing $category = 'faq' to some other category, or removing that part altogether and leaving it just as is_category() if you want it to work on ALL archives.

    I hope that helps!

    Lastly, are you a photographer by any chance??

    Hah, that’s one of my many hats, yes 🙂 Did my nickname right a bell?

    Thread Starter andynick

    (@andynick)

    Many thanks once again @mrfoxtalbot – that’s truly “above and beyond” – thank you!

    I was a little confused by the Vanilla custom fields – at first, I thought you were recommending another plugin (but then I woke up)!

    I’m deeply grateful for your patient help, and will get working on my pages very soon.

    I’ve been a keen photographer most of my life, and was professional for a number of years, before training dogs to herd sheep took over. Then the natural progression was to make videos about how to train sheepdogs!

    Thread Starter andynick

    (@andynick)

    @mrfoxtalbot – I’m sorry to be so dim, but I’m struggling with a few details regarding this pre_get_posts. For example:

    'andys_custom_post_order' presumably I need to enter something in here in place of “andys_custom_post_order”.

    My Custom Field is called “view-order” and the values go from 01 to 50-something.

    At the moment, I have the following in my Code Snippet (including the code which is already in there before you add your own). I left the category blank, as I realised the category was unimportant:

    add_action( 'wp_head', function () { ?>
    	<script>
    		
    		add_action( 'pre_get_posts', 'andys_custom_post_order' );
    function andys_custom_post_order( $query ) {
        // re-order front-end "faq" category archives using the value of the "my_post_order" post meta
        if( is_category() && !is_admin() && $query->is_main_query() ) {
            $query->set( 'posts_per_page','10' );
            $query->set( 'orderby','meta_value' );
            $query->set( 'meta_key','view-order' );
            $query->set( 'order','ASC' );
        }
    }
    
    	</script>
    <?php } );

    It’s not working at all, at the moment!

    Alvaro Gómez

    (@mrfoxtalbot)

    Hey @andynick! No worries, this can be trickier than one would think.

    Just to make sure we are on the same page, are using a plugin to add the value of these custom fields or you adding them using the “native/vanilla” method, as seen below?

    Screen Shot on 2022 06 23 at 14 03 49

    I am asking this because code I shared in my previous reply will work with the “native” custom fields.

    Thread Starter andynick

    (@andynick)

    Hello @mrfoxtalbot – thanks for getting back!
    Exactly as per your screenshot. I already have the Custom Field view-order at the foot of my posts and each post is allocated a number to represent the display order.
    I realised that the category is irrelevant in this case, so I want to drop that part and concentrate on the sort order.
    My confusion is about which parts of your code need changing (eg to view-order – or otherwise.

    Alvaro Gómez

    (@mrfoxtalbot)

    I see, thank you for confirming this. Your code looks good, @andynick.

    I did a quick test to use the exact code you shared on a testing site. I added view-order as a meta key to my posts, and it works fine. You can see it in action here (notice how the posts are not ordered by date):

    https://gutenbergcore.mystagingwebsite.com/category/faq/

    I am not quite sure what could be preventing the code from working. To troubleshoot this I would do the following:

    – Review that all your posts have the correct meta key and values.
    – Make sure that the code is active and is being executed.
    – Deactivate all plugins (except Code Snippets, if you are using it to insert this code).
    – Try activating a different theme.

    I hope that helps!

    Thread Starter andynick

    (@andynick)

    Aha!
    Thanks again @mrfoxtalbot. I think I may have jumped the gun!
    Your example posts are in reverse order – and mine may have been too. (I just saw they were not in the right order, and assumed the code wasn’t working).

    Ideally, I’d like number one to appear at the top, and so on. Could you suggest how I do that please?

    Thread Starter andynick

    (@andynick)

    Of course! ASC = Ascending, so DESC will mean descending order!

    Unfortunately, I still cannot get the snippet to work.

    I re-created the Code Snippet and it’s activated.
    I checked the Custom Fields on my posts – I even tried changing the Custom Field text “view-order” to “view_order” (underscore in place of hyphen).
    De-activated all plugins apart from Code Snippets.
    Tried a different theme (Elementor Lite, I think).

    Nothing changed. The first listed post has the view_order 30, the next 16 and the next 38.

    I also noticed that changing posts_per_page makes no difference either.

    Alvaro Gómez

    (@mrfoxtalbot)

    Thank you for trying all of the above and apologies for the belated reply @andynick!

    I am running out of ideas at this point, the only think that comes to mind would be server caching. Could it be the case that the company you are hosting your company with runs some server-side caching that cannot be cleared/flushed directly via your WP-Admin dashboard? I would recommend reaching out to them to confirm this so that we can rule out that possibility.

    If that does not work either, I would create a local duplicate of your site and try the code there. You can use All-in-One WP Migrationto create the backup and set up a local WP site using LocalWP or MAMP.

    Good luck!

    Thread Starter andynick

    (@andynick)

    I’m at a complete loss with this, @mrfoxtalbot – I’ve even been onto the server and deleted (or disabled) any cache I could find – but still no luck with it.

    I wonder if you’d mind going through the following code to be sure there’s nothing else that I should add or change..?

    <?php
    
    add_action( 'wp_head', function () { ?>
    	<script>
    		
    		add_action( 'pre_get_posts', 'andys_custom_post_order' );
    function andys_custom_post_order( $query ) {
        // re-order front-end "" category archives using the value of the "my_post_order" post meta
        if( is_category() && !is_admin() && $query->is_main_query() ) {
            $query->set( 'posts_per_page','15' );
            $query->set( 'orderby','meta_value' );
            $query->set( 'meta_key','view-order' );
            $query->set( 'order','DESC' );
        }
    }
    
    	</script>
    <?php } );

    This is the exact code I am trying. I put it into a Code Snippet. “Meta Key” presumably refers to the relevant Custom Field (which is called view-order).
    If you can’t find anything which needs changing, I’ll look at Local Hosting if you think it might help.
    Thanks once more for your patience…

    Alvaro Gómez

    (@mrfoxtalbot)

    Yeah, that does not look correct. If you are using Code Snippets to add this snippet, this should work:

    	
    add_action( 'pre_get_posts', 'andys_custom_post_order' );
    function andys_custom_post_order( $query ) {
        // re-order front-end "" category archives using the value of the "my_post_order" post meta
        if( is_category() && !is_admin() && $query->is_main_query() ) {
            $query->set( 'posts_per_page','15' );
            $query->set( 'orderby','meta_value' );
            $query->set( 'meta_key','view-order' );
            $query->set( 'order','DESC' );
        }
    }
Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Category List Order’ is closed to new replies.