2017-10-10 103 views
0

所以我想在WP中獲得自定義的發佈值。事情是我可以有多個自定義字段的值。所以我不想在代碼中進行硬編碼。我在想什麼做的是提供一個前綴的鍵值像WordPress的獲取自定義字段值

abc_email 
abc_website 

,所以我想用這個功能

get_post_meta 

或其他一些把所有的鍵,值對以abc開頭。這樣我可以在後端添加值,而不必更新我的代碼。一種方法是使用上述函數獲取所有元數據,然後遍歷它並過濾。但是,在我尋找的模式中,有沒有其他方式可以發送?

感謝

+1

你看過文檔嗎? https://developer.wordpress.org/reference/functions/get_post_meta/#comment-2124 –

回答

1

你可以直接查詢該數據庫包含前綴(相關表格等在這裏https://wordpress.stackexchange.com/questions/104434/where-are-custom-field-values-stored-in-the-database

  • 但是這是比較複雜的,還涉及到一個循環,不爲你提供任何你能一個條目公佈使用WP和PHP。如果你想,以滿足未知,你將有循環或interogate指數等

我可能會丟失你的問題的東西,但,這似乎直截了當:

您正在試圖避免這種情況:

process_field($abc_known1); 
process_field($abc_known2); 

// continually edit code for new entries e.g.: 
process_field($abc_NEW1); 

像這樣的事情將處理後來增加而不修改。

// get array of ALL cust fields for post 
$all_cust_fields = get_post_meta(get_queried_object_id()); 
foreach ($all_cust_fields as $key => $value) { 
    // some function for determing if $value starts with "abc_" 
    // if so either: 
    //  add to an "abc" array for later use/processing 
    // or take action in this loop e.g. 

    process_field($value); 
} 

我不確定您是否在討論鍵或值的前綴「abc_」,但同樣的原則適用。

0

你應該寫這樣的代碼,

$email_meta = get_post_meta('your_post_id', 'abc_email', true); 
$website_meta = get_post_meta('your_post_id', 'abc_website', true); 

或者

您可以按照這些鏈接,

example

example

希望這會對你有所幫助。