Just quick blog post this time! I have been working on a huge WordPress Multisite website today and am in the process of creating a plugin for the client which needed to work on all sub-sites in that Multisite install.
Needed a Custom Field from a Specific Page
Hooking into custom fields I needed to grab the field from the home page (as there was no need to set the key on any of the subsequent pages). As there are some 140+ sites, there was no way I could know what the ID of the home page was, especially when another site is added without my knowledge. I have lost count of the number of content publishers in this pretty hefty set-up!
Thankfully, they make this really easy to grab:
<?php // get the home page ID $home_page_id = get_option('page_on_front'); // pass that ID to get the custom field $custom_field = get_post_meta($home_page_id, 'key_name', true); ?>
Confirming the Home Page ID
Sometimes relying on code alone can be unnerving, so if you want to check the function has given you the correct Home Page ID (or
post_id more accurately) then just go to your Pages or Posts list and (if you’re on a desktop) hover over the title. The link looks like this:
The post=2 is the number which should correspond to the output from
Not Just for Multisite
In reality, the
get_option function is not a multisite function so you can use it on a bog-standard website too. But it’s only when you’re presented with a problem that you have to look how to fix it. So you can use this on any WordPress website, in reality!