Forum Replies Created

Viewing 15 replies - 1 through 15 (of 28 total)
  • Thread Starter mattspace

    (@mattspace)

    Right, so we have the ability to not everything as a filter to leave the thing we want, but lack the ability to specify the thing we want from the outset?

    If you wouldn’t mind, would you set out the combined version with the ORs, so I can see what the syntax difference is?

    thanks.

    Thread Starter mattspace

    (@mattspace)

    @threadi

    I tried your code example:

    function custom_sort_for_post_archive( $query ) {
    // ignore in the backend.
    if( is_admin() ) {
    return $query;
    }

    // ignore if this is not the main query.
    if( ! $query->is_main_query() ) {
    return $query;
    }

    // ignore if this is not an archive.
    if( ! $query->is_archive() ) {
    return $query;
    }

    // ignore it if this is not the category.
    if( 'photostream' !== $query->query_vars['category_name'] ) {
    return $query;
    }

    // change the sort to order by modified date.
    $query->set('orderby', 'post_modified');

    // always return
    return $query;
    }
    add_action( 'pre_get_posts', 'custom_sort_for_post_archive' );

    …and it seems to be working as desired. If I look at the RSS feed, it’s showing the posts in the order I want. Small snag was encountered though, as it seems my RSS reader app insists on sorting things by strict publication date.

    Does this function going through all these if not steps add an overhead to each page load on the site, that I might have avoided by just making a custom template .php file for the photostream category?

    thanks.

    Thread Starter mattspace

    (@mattspace)

    That’s interesting, when you say the ID isn’t available, I have functions that remove post categories from both the home page, and the RSS feed, and they target category IDs:

    // This function excludes posts of the diary categories from the home page feed

    function mg_exclude_category_home( $query ) {
    if ( $query->is_home ) {
    $query->set( 'cat', '-97, -95, -93, -81, -80, -109, -112, -117, -123' );
    }
    return $query;
    }

    add_filter( 'pre_get_posts', 'mg_exclude_category_home' );

    // This function excludes diary category posts from the RSS feed

    function exclude_category( $query ) {
    if ( $query->is_feed ) {
    $query->set('cat', '-97, -95, -93, -81, -80, -109, -112, -117, -123');
    }
    return $query;
    }

    add_filter('pre_get_posts', 'exclude_category');

    …but is the issue that the targeting of the function:

    if ( $query->is_home ) {

    … or:

    if ( $query->is_feed ) {

    …are specifically done with recognised fixed landmark targets hardcoded into WP – home and feed. Is there not some equivalent for explicitly naming the category by name, slug, or ID? Something like:

    function custom_sort_for_photostream( $query ) {
    if ( $query->is_archive( 120 ) ) { // can we identify by ID or slug here ?

    // or should it use is_category ?
    // or is_whichever( 'cat', '120') ?


    $query->set('orderby', 'post_modified');
    }
    }
    add_action( 'pre_get_posts', 'custom_sort_for_photostream' );

    …or is there another way to get the category id?

    thanks – learning this stuff bit by bit.

    Thread Starter mattspace

    (@mattspace)

    Thanks for that – so if I wanted to restrict this to a particular category’s archive / feed, eg it only applies for the category ID 120, how do I add that specificity?

    Is there a version of it that looks something like this:

    function custom_sort_for_photostream() {
    if( is_archive( 120 ) ) {
    set('orderby', 'post_modified');
    }
    }
    add_action( 'pre_get_posts', 'custom_sort_for_photostream' );
    Thread Starter mattspace

    (@mattspace)

    The publish date IS being used for sorting. That’s the problem I’m trying to solve.

    I want it to order posts on the page, and in the feed for that category by modified date.

    So a post I add today, with a published date of last year, shows up as more recent than a post I added last week, with a published date of last month.

    Thread Starter mattspace

    (@mattspace)

    much appreciated πŸ™‚

    Thread Starter mattspace

    (@mattspace)

    Right, so conceptually I should think of things as if the parent and child themes are hardcoded to require being in the same directory, and speak to my webhost about whether I can symlink from the first site’s /themes directory, to the second?

    So I’d have:

    • /public_html/site_one/wp-content/themes/parent_theme
    • /public_html/site_two/wp-content/themes/parent_theme(symlink)
    • /public_html/site_two/wp-content/themes/child_theme

    Is that the easiest / best way to go about it?

    Thanks.

    Thread Starter mattspace

    (@mattspace)

    Your assumption is correct πŸ™‚

    Excellent, in that case I’ll mark the question as resolved. Much appreciated.

    Cheers πŸ™‚

    Thread Starter mattspace

    (@mattspace)

    haha, nice catch, late night message replying, all my php code is <?php etc in my theme files.

    But yes, that’s my level of php fluency that I didn’t recognise it myself.

    I managed to get it all working, after figuring out what could and could not sit within echo’d html. This isn’t throwing up any errors, and produces a wonderful conditional result where there’s nothing output on posts where I haven’t added the custom field:

    function mytheme_get_the_original_date() {
    $first = get_post_meta( get_the_ID(), 'first_published_date', true );
    if ( ! empty( $first )) {
    echo '<p class="entry-meta entry-meta-footer mod-date">
    <span class="pre-in first-published">' , $first , '</span>
    <span class="in">: Initial</span>
    </p>';
    }
    }

    Assuming that’s all good and correct, thankyou so much for bearing with me on this little learning journey. It’s really been a significant improvement in my understanding of the system.

    Thread Starter mattspace

    (@mattspace)

    Oh so I was correct to put the echo into the curly braces for the functions.php part? If so, that was a bit of a conceptual leap for me, so I really appreciate your patience in answering my questions on this.

    Thankfully, I’m the only person who needs to deal with my code or my themes, but I do need to deal with it every few years, and coming back to it a couple of years later, being able to just see:

    <php? mytheme_get_the_original_date() ?>

    …is probably a little cleaner for me when reading it. I’m mostly editing my template files from a what-goes-where perspective, not a how-it-works one.

    I find stuff being in functions.php is easier to deal with when building it up myself, but is harder to understand what a theme is doing, when trying to reverse-engineer it for customisation etc. That’s largely why I spent a couple of years on and off trying to build an entire new theme from scratch – so I’d know how every bit of it worked, and could document that in the comments.

    Thread Starter mattspace

    (@mattspace)

    For perspective, I’m coding my themes from scratch, as .php classic template files.

    One thing I found worked well for me cognitively with the programming theory lessons I was doing (Swift Playgrounds), was creating custom functions so that instead of putting all the functional code inline at the point it’s used, you put all that in the non-running part of the file in discreet units of functionality, with an arbitrary name, and then at the place you want it to run, you just have the function name reference.

    So the code I made ended up being effectively just a running list of references.

    It made sense to me from a compartmentalisation perspective, and I’ve tended to do that with functions in my wordpress theme building, so I try to have:

    <php? my_utility_function() ?>

    …and then all the actual stuff it does kept in the functions.php file. Buuut I know so little about how wordpress and php function that I really only learn things by seeing the completed example – I don’t have the knowledge base to build up, but I can backfill that knowledge by working backwards from the completed example (like the ones provided in this thread). Thats just how I learn if I’m not in a classroom with a live tutor.

    So to my way of how I like to do things, I’d want to make the template file have something like this:

    <!-- begin originally published date entry -->
    <php? mytheme_get_the_original_date() ?>
    <!-- end originally published date entry -->

    So, would this be close to the answer for functions.php:

    function mytheme_get_the_original_date() {
    $first = get_post_meta( get_the_ID(), 'first_published_date', true );
    if ( ! empty( $first )) {
    echo "First published: $first";
    }
    }
    Thread Starter mattspace

    (@mattspace)

    Oh thanks… so if I’m interpreting this correctly (I only have the faintest knowledge of programming, but I’m trying to learn)…

    You’re starting by declaring a variable (that’s just an arbitrary name, not a special wordpress signifier?), whose content is filled by the php command to get the contents of the custom field, and then testing if the field’s value is NOT empty to echo a string “First Published:” followed by the contents of the variable. Is that correct?

    That seems fairly comprehensible. Would it be better practice to have that implemented in the funchtions.php file (if you wouldn’t mind writing the code for that form of it), and just have a call to that function in my template file?

    Thanks – that’s a solid new thing (setting an retrieving variables in php) I learned.

    Thread Starter mattspace

    (@mattspace)

    Bearing in mind I’m operating at the edge of my knowledge here…

    If you’re simply going to display the field, you can save it in any format you wish.

    OK, so having tested it, it’s literally just an arbitrary text string, rather than a date calculation call. That’s workable for me.

    Is there a way I can make this happen so the text string “First Published:” doesn’t have to be in the custom field value itself?

    e.g is there an addition that can be made to:

    <?php echo get_post_meta( get_the_ID(), 'first_published_date', true ); ?>

    … which lets me put an echo of “First Published:” inside this, so it only shows if the first_published_date field has a recorded value?

    Thread Starter mattspace

    (@mattspace)

    Could you please provide some more specificity for that? I add a new custom field to the post with:

    name: first_published_date

    value: do I need to enter the date in a specific format yyyy/mm/dd etc so that it matches whatever date style I’m using in the template to show the published date; eg:

    <?php the_time('Y/m/d') ?>

    The reason I’m changing the published date is to make a particular post more recent than a subsequent one, so when they’re both sticky, the older post shows up above the newer one in a descending by date home page… so how does this date interact with that, or do I still change the post date to control the page order, and this is just to get the decorative originally posted date that I set with the custom field?

    Thread Starter mattspace

    (@mattspace)

    Just because things are changed, doesn’t mean they’re fixed. IMHO apart from security fixes, almost everything WP has done since 4.8 has been to the platform’s detriment. What you call “fixes”, I call “breaks”.

Viewing 15 replies - 1 through 15 (of 28 total)