{"id":14649,"date":"2013-05-29T10:59:38","date_gmt":"2013-05-29T10:59:38","guid":{"rendered":"https:\/\/es.wordpress.org\/support\/topic\/ordenar-posts-por-tipos-de-post-personalizados-en-un-boton-select\/"},"modified":"2016-08-06T00:19:38","modified_gmt":"2016-08-06T00:19:38","slug":"ordenar-posts-por-tipos-de-post-personalizados-en-un-boton-select","status":"closed","type":"topic","link":"https:\/\/es.wordpress.org\/support\/topic\/ordenar-posts-por-tipos-de-post-personalizados-en-un-boton-select\/","title":{"rendered":"Ordenar posts por tipos de post personalizados en un bot\u00f3n select"},"content":{"rendered":"<p>Buenas!<\/p>\n<p>En el menu de administraci\u00f3n de WordPress tengo creados varios \u00abcustom-posts\u00bb cada uno con sus posts. Ahora en uno de esos post personalizados he creado un campo que recupera todos los posts de los posts personalizados. El problema es que necesitar\u00eda que los ordenara de la siguiente manera:<\/p>\n<p>Post Personalizado 1:<br \/>\nPosts<br \/>\nPost Personalizado 2:<br \/>\nPosts<br \/>\n&#8230;<\/p>\n<p>Os paso el c\u00f3digo que crea el campo y recupera y guarda los datos de select:<\/p>\n<pre><code>\/\/ Add the Meta Box  \n\nfunction add_custom_meta_box_related() {\n    add_meta_box(\n        &#039;custom_meta_box_related&#039;, \/\/ $id\n        &#039;Related Information&#039;, \/\/ $title\n        &#039;show_custom_meta_box_related&#039;, \/\/ $callback\n        &#039;related&#039;, \/\/ $page\n        &#039;normal&#039;, \/\/ $context\n        &#039;high&#039;); \/\/ $priority\n}\nadd_action(&#039;add_meta_boxes&#039;, &#039;add_custom_meta_box_related&#039;);\n\n    \/\/ Field Array\n    $prefix_related = &#039;custom_&#039;;\n    $custom_meta_fields_related = array(  \n\n\t\tarray(\n\t\t\t&#039;label&#039; =&gt; &#039;Related Items&#039;,\n\t\t\t&#039;desc&#039; =&gt; &#039;Select a related item(s)&#039;,\n\t\t\t&#039;id&#039;    =&gt;  $prefix_materiales.&#039;post_id&#039;,\n\t\t\t&#039;type&#039; =&gt; &#039;post_list&#039;,\n\t\t\t&#039;post_type&#039; =&gt; array(&#039;products&#039;,&#039;paso1&#039;,&#039;paso2&#039;,&#039;paso3&#039;,&#039;paso4&#039;,&#039;compra&#039;),\n)\n    );  \n\n\t\/\/ The Callback\nfunction show_custom_meta_box_related() {\nglobal $custom_meta_fields_related, $post;\n\/\/ Use nonce for verification\necho &#039;&lt;input type=&quot;hidden&quot; name=&quot;custom_meta_box_nonce&quot; value=&quot;&#039;.wp_create_nonce(basename(__FILE__)).&#039;&quot; \/&gt;&#039;;  \n\n    \/\/ Begin the field table and loop\n    echo &#039;&lt;table class=&quot;form-table&quot;&gt;&#039;;\n    foreach ($custom_meta_fields_related as $field_related) {\n        \/\/ get value of this field if it exists for this post\n        $meta_related = get_post_meta($post-&gt;ID, $field_related[&#039;id&#039;], true);\n        \/\/ begin a table row with\n        echo &#039;&lt;tr&gt;\n                &lt;th&gt;&lt;label for=&quot;&#039;.$field_related[&#039;id&#039;].&#039;&quot;&gt;&#039;.$field_related[&#039;label&#039;].&#039;&lt;\/label&gt;&lt;\/th&gt;\n                &lt;td&gt;&#039;;\n                switch($field_related[&#039;type&#039;]) {\n                    \/\/ case items will go here \n\n\t\t\t\t\t\/\/ post_list\n\t\t\t\t\tcase &#039;post_list&#039;:\n\t\t\t\t\t$items = get_posts( array (\n\t\t\t\t\t\t&#039;post_type&#039; =&gt; $field_related[&#039;post_type&#039;],\n\t\t\t\t\t\t&#039;posts_per_page&#039; =&gt; -1\n\t\t\t\t\t));\n\t\t\t\t\techo &#039;&lt;select multiple name=&quot;&#039;.$field_related[&#039;id&#039;].&#039;&quot; id=&quot;&#039;.$field_related[&#039;id&#039;].&#039;&quot;&gt;\n\t\t\t\t\t&lt;option value=&quot;&quot;&gt;Select One or more&lt;\/option&gt;&#039;; \/\/ Select One\n\t\t\t\t\tforeach($items as $item) {\n\t\t\t\t\t\techo &#039;&lt;option value=&quot;&#039;.$item-&gt;ID.&#039;&quot;&#039;,$meta_related == $item-&gt;ID ? &#039; selected=&quot;selected&quot;&#039; : &#039;&#039;,&#039;&gt; &#039;.$item-&gt;post_title.&#039;&lt;\/option&gt;&#039;;\n\t\t\t\t\t} \/\/ end foreach\n\t\t\t\t\techo &#039;&lt;\/select&gt;&lt;br \/&gt;&lt;span class=&quot;description&quot;&gt;&#039;.$field_related[&#039;desc&#039;].&#039;&lt;\/span&gt;&#039;;\n\t\t\t\t\tbreak;\n                } \/\/end switch\n        echo &#039;&lt;\/td&gt;&lt;\/tr&gt;&#039;;\n    } \/\/ end foreach\n    echo &#039;&lt;\/table&gt;&#039;; \/\/ end table\n}\n\n\/\/ Save the Data\nfunction save_custom_meta_related($post_id) {\n    global $custom_meta_fields_related;  \n\n    \/\/ verify nonce\n    if (!wp_verify_nonce($_POST[&#039;custom_meta_box_nonce&#039;], basename(__FILE__)))\n        return $post_id;\n    \/\/ check autosave\n    if (defined(&#039;DOING_AUTOSAVE&#039;) &amp;&amp; DOING_AUTOSAVE)\n        return $post_id;\n    \/\/ check permissions\n    if (&#039;page&#039; == $_POST[&#039;post_type&#039;]) {\n        if (!current_user_can(&#039;edit_page&#039;, $post_id))\n            return $post_id;\n        } elseif (!current_user_can(&#039;edit_post&#039;, $post_id)) {\n            return $post_id;\n    }  \n\n    \/\/ loop through fields and save the data\n    foreach ($custom_meta_fields_related as $field_related) {\n        $old = get_post_meta($post_id, $field_related[&#039;id&#039;], true);\n        $new = $_POST[$field_related[&#039;id&#039;]];\n        if ($new &amp;&amp; $new != $old) {\n            update_post_meta($post_id, $field_related[&#039;id&#039;], $new);\n        } elseif (&#039;&#039; == $new &amp;&amp; $old) {\n            delete_post_meta($post_id, $field_related[&#039;id&#039;], $old);\n        }\n    } \/\/ end foreach\n}\nadd_action(&#039;save_post&#039;, &#039;save_custom_meta_related&#039;);<\/code><\/pre>\n<p>Supongo que en este punto<\/p>\n<pre><code>\/\/ post_list\ncase &#039;post_list&#039;:\n$items = get_posts( array (\n&#039;post_type&#039; =&gt; $field_related[&#039;post_type&#039;],&#039;posts_per_page&#039; =&gt; -1));\necho &#039;&lt;select multiple name=&quot;&#039;.$field_related[&#039;id&#039;].&#039;&quot;id=&quot;&#039;.$field_related[&#039;id&#039;].&#039;&quot;&gt;\n&lt;option value=&quot;&quot;&gt;Select One or more&lt;\/option&gt;&#039;; \/\/ Select One\nforeach($items as $item) {\necho &#039;&lt;option value=&quot;&#039;.$item-&gt;ID.&#039;&quot;&#039;,$meta_related == $item-&gt;ID ? &#039;selected=&quot;selected&quot;&#039; : &#039;&#039;,&#039;&gt; &#039;.$item-&gt;post_title.&#039;&lt;\/option&gt;&#039;;\n} \/\/ end foreach<\/code><\/pre>\n<p> tengo que requerir el id de cada post personalizado pero no se me ocurre c\u00f3mo.<\/p>\n<p>Gracias de antemano<\/p>\n","protected":false},"template":"","class_list":["post-14649","topic","type-topic","status-closed","hentry","topic-tag-campo-personalizado","topic-tag-custom-field"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/es.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/es.wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/es.wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/es.wordpress.org\/support\/wp-json\/wp\/v2\/topic\/14649\/revisions"}],"wp:attachment":[{"href":"https:\/\/es.wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=14649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}