1

I want to create a custom post type for store that having all store names and it is working. Now I want to fetch the all stores in a select box in every new post of WordPress, then selected store name will used as custom field for that post. I want to save this store name as meta value for this post in wp_postmeta table.

Every custom post having the post_type='store' so how I write a function to fetch all post by store type.

1 Answer 1

0

To get the posts from a post type you can use get_posts()

$posts = get_posts(
    array (
        'numberposts'    => -1,
        'posts_per_page' => -1,
        'post_type'      => 'store'
    )
);

Be aware, the -1 is dangerous: If there are millions of stores, the query might run into a time-out.

Here is a very basic example for such a meta box. You will find much more examples and explanations in our tag .

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Store metabox */

add_action( 'add_meta_boxes_post', 'wpse_98184_register_store_metabox' );
add_action( 'save_post', 'wpse_98184_save_store_metabox' );

function wpse_98184_register_store_metabox()
{
    add_meta_box( 'post_store', 'Store', 'wpse_98184_render_store_metabox', NULL, 'side' );
}

function wpse_98184_render_store_metabox( $object, $box )
{
    $nonce = wp_create_nonce( __FILE__ );
    echo "<input type='hidden' name='nonce_store_mbox' value='$nonce' />";

    $posts = get_posts(
        array (
            'numberposts'    => -1,
            'posts_per_page' => -1,
            'post_type'      => 'store',
        )
    );

    if ( ! $posts )
        return print 'No store found';

    $meta    = get_post_meta( $object->ID, '_store', TRUE );
    $selected = $meta ? $meta : 0;
    $html     = '<select name="_store"><option value="0">Select a store</option>';

    foreach ( $posts as $post )
        $html .= sprintf(
            '<option value="%1$d" %2$s>%3$s</option>',
            esc_attr( $post->ID ),
            selected( $post->ID, $selected, FALSE ),
            esc_html( $post->post_title )
            );

    $html .= '</select>';

    echo $html;
}

function wpse_98184_save_store_metabox( $id )
{
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;

    if ( ! isset ( $_POST[ 'nonce_store_mbox' ] ) )
        return;

    if ( ! wp_verify_nonce( $_POST[ 'nonce_store_mbox' ], __FILE__ ) )
        return;

    if ( ! current_user_can( 'edit_post', $id ) )
        return;

    if ( ! isset ( $_POST['_store'] ) )
        return delete_post_meta( $id, '_store' );

    update_post_meta( $id, '_store', $_POST['_store'] );
}

To list all these associated posts in a store page, use a function that queries for post meta fields:

function wpse_98184_list_store_posts()
{
    $args = array(
        'post_type'  => 'post',
        'numberposts'    => -1,
        'posts_per_page' => -1,
        'meta_query' => array (
            array (
                'key' => '_store',
                'value'   => get_the_ID(),
                'compare' => '==',
            ),
        )
    );

    $posts = get_posts( $args );

    if ( ! $posts )
        return '';

    $output = '<ul class="store-posts">';

    foreach ( $posts as $post )
        $output .= sprintf(
            '<li><a href="%1$s">%2$s</a></li>',
            get_permalink( $post->ID ),
            esc_html( get_the_title( $post->ID ) )
        );

    $output .= '</ul>';

    return $output;
}

You could use this function as a shortcode handler:

add_shortcode( 'storeposts', 'wpse_98184_list_store_posts' );

Then just add [storeposts] wherever you need that list in your store.

7
  • OK. How to add or call post type store as meta box in new post page. and save it to the wp_postmeta.
    – Sachin
    Commented May 3, 2013 at 11:16
  • @Sachin See my update.
    – fuxia
    Commented May 3, 2013 at 13:01
  • Thanks toscho, It's really help me. and got the solution. One more point if you don't mind! As you given solution the name of store get from the meta_value = post_id; Now I have a store page suppose www.xyz.com/store-name/ [URL as from custom post type] I need to fetch the all post that having the store-name in the custom field so how to fetch it to the custom post type page.
    – Sachin
    Commented May 3, 2013 at 13:34
  • @Sachin If I were pedantic, I would say that’s a new question. :) But I’m not, so I just made an update to my answer.
    – fuxia
    Commented May 3, 2013 at 13:49
  • Thanks toscho. your post really helping me. Where do I place this given code to list the all post, in single.php or category.php or something anywhere which page should be edited for custom post type pages. As I am newbie in programming as well this forum too so I don't know I need to ask a new question after finishing one. Sorry Again a question.
    – Sachin
    Commented May 3, 2013 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.