Marketing
Advertisement
  • Home
  • News
  • B2B
  • B2C
  • Marketing Automation
  • Market Research
  • Contact
No Result
View All Result
  • Home
  • News
  • B2B
  • B2C
  • Marketing Automation
  • Market Research
  • Contact
No Result
View All Result
Marketing News Hubb
No Result
View All Result

WordPress: How To Add A Sortable Column To Display A Custom Field On The Custom Post Type Admin Page

May 14, 2023
Home Marketing Automation


One area of Martech Zone that continues to drive a lot of traffic to my site is my growing documentation of sales, marketing, and technology acronyms. I continue to expand the list of almost 600 and I also tag the posts with the acronym so that the latest posts are displayed within the single acronym display page.

This custom post type I created has three critical elements:

  • Title – the acronym itself.
  • Definition – what the acronym stands for.
  • Content – the actual description of the acronym.

With WordPress, the title and content are included elements to any post type, so the definition had to be added via a custom field that’s incorporated via Meta Box. There’s one outstanding issue, though, and that’s displaying the definition on the admin page where all of my Acronyms are listed. 

Within your functions.php file, you can add a custom field to your admin columns. In this case, I’m only doing it for the acronym custom post type. You’ll want to update the textdomain in your code for your theme or child theme.

// Add a new 'Definition' column to the Acronym post list
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
function add_definition_column_to_acronym_list($columns) {
    $new = array();
    foreach($columns as $key => $title) {
        if ($key == 'title') // Put the Definition column after the Title column
            $new['acronym_definition'] = __( 'Definition', 'textdomain' );
        $new[$key] = $title;
    }
    return $new;
}

// Fill the new 'Definition' column with the values from 'acronym_definition' custom field
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
function add_definition_column_content_to_acronym_list($column, $post_id) {
    if ($column == 'acronym_definition') {
        $definition = get_post_meta($post_id, 'acronym_definition', true);
        if (!empty($definition)) {
            echo $definition;
        } else {
            echo __('No definition', 'textdomain');
        }
    }
}

This adds the column as the first column on the admin page. I actually would like it to be the second column, so I modified the code to add the column after the title column.

// Add a new 'Definition' column to the Acronym post list
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
function add_definition_column_to_acronym_list($columns) {
    $new_columns = array();

    foreach($columns as $key => $value) {
        $new_columns[$key] = $value;

        if ($key === 'title') {
            $new_columns['acronym_definition'] = __('Definition', 'textdomain');
        }
    }

    return $new_columns;
}

// Fill the new 'Definition' column with the values from 'acronym_definition' custom field
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
function add_definition_column_content_to_acronym_list($column, $post_id) {
    if ($column === 'acronym_definition') {
        $definition = get_post_meta($post_id, 'acronym_definition', true);
        echo $definition ? $definition : __('No definition', 'textdomain');
    }
}

Now I can easily navigate through my acronyms and view the definition of them:

This added the column but didn’t make it sortable. To make it sortable, the code can include the sortable element as well as the query that’s needed to prefetch the list:

// Add a new 'Definition' column to the Acronym post list
add_filter('manage_acronym_posts_columns', 'add_definition_column_to_acronym_list');
function add_definition_column_to_acronym_list($columns) {
    $new_columns = array();

    foreach($columns as $key => $value) {
        $new_columns[$key] = $value;

        if ($key === 'title') {
            $new_columns['acronym_definition'] = __('Definition', 'textdomain');
        }
    }

    return $new_columns;
}

// Fill the new 'Definition' column with the values from 'acronym_definition' custom field
add_action('manage_acronym_posts_custom_column', 'add_definition_column_content_to_acronym_list', 10, 2);
function add_definition_column_content_to_acronym_list($column, $post_id) {
    if ($column === 'acronym_definition') {
        $definition = get_post_meta($post_id, 'acronym_definition', true);
        echo $definition ? $definition : __('No definition', 'textdomain');
    }
}

// Make the 'Definition' column sortable
add_filter('manage_edit-acronym_sortable_columns', 'make_definition_column_sortable');
function make_definition_column_sortable($columns) {
    $columns['acronym_definition'] = 'acronym_definition';
    return $columns;
}

// Customize the query that sorts the 'Definition' column
add_action('pre_get_posts', 'sort_definition_column');
function sort_definition_column($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    if ($query->get('orderby') == 'acronym_definition') {
        $query->set('meta_key', 'acronym_definition');
        $query->set('orderby', 'meta_value');
    }
}



Source link

Previous Post

What Makes A Future Fit Technology Strategy?

Next Post

Elon Musk names new Twitter CEO: Linda Yaccarino

Next Post

Elon Musk names new Twitter CEO: Linda Yaccarino

Recommended.

Enable welcomes Mark Gilham as Director & Evangelist

August 7, 2023

Enable Now Available Through Microsoft AppSource and Azure Marketplace

May 12, 2023

Product Mix Rebates: An Effective Strategy for Boosting Sales Volume and Profits

May 19, 2023

© Marketing News Hubb All rights reserved.

Use of these names, logos, and brands does not imply endorsement unless specified. By using this site, you agree to the Privacy Policy and Terms & Conditions.

Navigate Site

  • Home
  • News
  • B2B
  • B2C
  • Marketing Automation
  • Market Research
  • Contact

Newsletter Sign Up.

No Result
View All Result
  • Home
  • News
  • B2B
  • B2C
  • Marketing Automation
  • Market Research
  • Contact

© 2022 Marketing News Hubb All rights reserved.

Situs Slot Online Terbaik Slot Gacor Hari Ini Pragmatic Situs Slot Gacor Terbaru Situs Slot Gacor Demo Slot Pragmatic Play RTP Slot Gacor Pragmatic Play RTP Slot Slot Gacor Gampang Menang Link Slot Gacor Slot Gampang Maxwin https://water.news.energy-water.com/slot-online-jackpot-terbesar/ https://www.chesapeakemarineinst.com/slot-online-terpercaya/ https://financenewshubb.com/slot-gacor/ https://www.aydinemlaktrabzon.com/slot-gacor/ https://biomedanas.com/slot-gacor/ https://vrcstoke.co.uk/link-slot-gacor/ https://www.bencooperphotography.com/slot-anti-rungkad/ https://pavimentoencarreteras.com/slot-gacor/ https://www.casadanihotel.com/slot-gacor/ https://eksukoon.com/slot-gacor/ https://bagliography.com/slot-gacor/ https://hundopi.se/slot-gacor/ https://numb-z.com/wp-includes/slot-gacor-gampang-menang/ https://perfectlypolisheddayspa.com/slot-gacor-hari-ini/ https://www.gradiloneimballaggi.it/slot-gacor/ https://azbba.org/wp-includes/slot-gacor/ https://texasheritageforliving.com/texas-living/texas-sayings/ https://www.dermatude-shop.nl/wp-includes/slot-gacor/
https://athleticssportsandperformance.com/slot-online-gacor/
https://www.adeccogroup.com.ar/situs-slot-online-gacor-pragmatic-gampang-maxwin/
https://calculatuiva.cl/link-slot-gacor/
https://www.edenvaleriverwatch.co.za/slot-gampang-menang/
https://canadianbuyers.club/wp-includes/slot-gacor/
https://vicdigital.it/slot-gacor/
https://nexarkagroup.com/slot-anti-rungkad/
https://dailyaddictiontreatment.com/situs-slot-online-jackpot-terbesar/
https://studytutee.com/wp-includes/slot-gacor/
https://northernrevive.co.uk/slot-gacor/
https://t20.com.pk/slot-gacor/
https://thekingzcart.com/slot-gacor/
https://iavstudios.com/wp-admin/slot-gacor/
https://www.hartwellclothing.com/wp-content/uploads/slot-gacor/
https://upgradedtamilan.com/slot-gacor/
https://janestrinket.com/slot-gacor/
https://bizcoachng.com/wp-includes/slot-gacor/
https://www.seacliffapartments.com/idn-poker-online/
https://royalmeg.com/wp-content/slot-gacor/
https://mwe100.com/poker-online/
https://newsarkariyojana.in/e-district-assam-login-registration-edistrict-assam-gov-in-status/
https://simplygroup.it/poker-online/
https://es.menshoppingguide.com/slot-maxwin/
https://www.non-fumeur.fr/slot-gacor-maxwin/
https://www.nftsmag.net/slot-gacor-maxwin/
https://depilacionlaserelche.es/wp-includes/slot-maxwin/
https://www.baccarato.org/slot-maxwin/
https://fotboll-idag.se/slot-gacor-maxwin/
https://holdingbolag.se/slot-gacor-maxwin/
Demo Slot Maxwin
Demo Slot Maxwin
Demo Slot
https://prolesanpure.se/slot-terbaru/
https://garminfenix5.com/slot-pragmatic/
https://cfe-recibo.com/situs-judi-slot-terbaik-dan-terpercaya-no-1/
https://miportalfonesep.mx/slot-bonus-new-member/

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

Slot Gacor Maxwin

https://aiplgurugram.com/nowyouseeme/

https://akshiyachettinadsnacks.com/slot-gacor/

https://booking.grandroyaltravel.com/wp-includes/slot-gacor-maxwin/

https://edacco.org/connect-ed/

https://shivasomadev.com/wp-includes/slot-gacor/

https://insenergias.org/slot-gacor/

https://luxesalon.ie/slot-gacor-anti-rungkad-hari-ini/

https://planetaeducativo.com/wp-includes/slot-gacor/

https://www.decolanet.com.br/slot-gacor/

https://armina.bio/slot-gacor-anti-rungkad/

https://www.crunchylivinmamastyle.com/slot-gacor/

http://www.radiodontica.com.br/slot-gampang-menang/

https://www.filmrumor.com/situs-judi-slot-online%20terbaik%20-terpercaya/

https://www.stelliumoffersrack.com/slot-gacor-terbaik-dan-terpercaya-no-1/

https://carlamormon.com/slot-gacor/

https://blossom.works/wp-includes/slot-gacor/

https://amosfitnessandfood.com/situs-judi-slot-online/

https://www.visitesgratuites.fr/slot-gacor/

https://roynalrainline.com/slot-online/

https://disinfect.se/wp-includes/judi-slot-online-jackpot-terbesar/

https://proctologonavarra.com/link-slot-gacor/