I took your advice and with a little more time researching the I got it working the way I intended, thanks a lot!
I found a code snippet from github that will help me create pages without actually creating them, here it is:
// create fake page called "chat-room"
// modify function and variable names with "ABCD" to whatever you like
// modify variable $fakepage_ABCD_url to the fake URL you require
add_filter('the_posts','fakepage_ABCD_detect',-10);
function fakepage_ABCD_detect($posts){
global $wp;
global $wp_query;
global $fakepage_ABCD_detect; // used to stop double loading
$fakepage_ABCD_url = "chat-room"; // URL of the fake page
if ( !$fakepage_ABCD_detect && (strtolower($wp->request) == $fakepage_ABCD_url || $wp->query_vars['page_id'] == $fakepage_ABCD_url) ) {
// stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
$fakepage_ABCD_detect = true;
// create a fake virtual page
$post = new stdClass;
$post->post_author = 1;
$post->post_name = $fakepage_ABCD_url;
$post->guid = get_bloginfo('wpurl') . '/' . $fakepage_ABCD_url;
$post->post_title = "Page Title";
$post->post_content = fakepage_ABCD_render();
$post->ID = -1;
$post->post_type = 'page';
$post->post_status = 'static';
$post->comment_status = 'closed';
$post->ping_status = 'open';
$post->comment_count = 0;
$post->post_date = current_time('mysql');
$post->post_date_gmt = current_time('mysql', 1);
$posts=NULL;
$posts[]=$post;
// make wpQuery believe this is a real page too
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
unset($wp_query->query["error"]);
$wp_query->query_vars["error"]="";
$wp_query->is_404=false;
}
return $posts;
}
function fakepage_ABCD_render(){
return "My amazing pretend page :D";
}
Please help me figure out how to change the url, title, and get the custom fields I want dynamically for 2 pages I choose.
Thanks!
Oh I see the solution, a little bit messy but I get the point, thanks for finding it (:
You are a life saver.
It’s so much fun to see something works after a few hours of struggling with.
Thanks a lot this works perfectly!
I didn’t quite understand you, where do you see this difference?
Here’s the entire code I have till now:
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title', 'trans' ),
'number' => __( 'Number', 'trans' ),
'author' => __( 'Author', 'trans' ),
'Date' => __( 'Date', 'trans' ),
);
return $cols;
}
add_filter( "manage_edit-usa-locations_columns", "change_columns" );
function columns_content_only_locations($column_name, $post_ID) {
if ($column_name == 'number') {
$locations = get_posts(array(
'post_type' => 'post',
'category' => '3',
'numberposts'=>-1,
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'headquarters', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
$totalCount = count( $locations );
echo $totalCount;
update_post_meta($post_ID, 'count', $totalCount);
}
}
add_action('manage_usa-locations_posts_custom_column', 'columns_content_only_locations', 10, 2);
// Make these columns sortable
function sortable_columns() {
return array(
'number' => 'number',
'title' => 'title',
);
}
add_filter( "manage_edit-usa-locations_sortable_columns", "sortable_columns" );
function number_column_order( $vars ) {
if ( isset( $vars['orderby'] ) && 'count' == $vars['orderby'] ) {
$vars["meta_key"] = "count";
$vars["order"] = "ASC";
$vars["orderby"] = "meta_value_num";
}
return $vars;
}
add_filter( 'request', 'number_column_order' );
I tried using $vars["orderby"] = "meta_value_num";
But I get the same result, still weird order =/
This is a custom post type for USA states and it will always stay 50 I won’t have more pages there as there is no more states 🙂
I have set the number of items on page to 999 so it won’t be paginated.
Using “LIKE” with this function will slow down my site or this specific page in the admin panel you mean? because everything seems to be quick and works great..
I’m not sure what you’re saying is a good way to go by, seems to be too complicated for such a thing, is it really necessary? I tried something else which seems to work when I echo the meta_value from the WordPress default custom field.
Here’s my code for this now, just added the last update_post_data line.
function columns_content_only_locations($column_name, $post_ID) {
if ($column_name == 'number') {
$locations = get_posts(array(
'post_type' => 'post',
'category' => '3',
'numberposts'=>-1,
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'headquarters', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
$totalCount = count( $locations );
echo $totalCount;
update_post_meta($post_ID, 'count', $totalCount);
}
}
And my sorting function is there but it sorts in a weird order I can’t figure what it is, when I sort it goes always to to the same order: 2, 6, 15, 4, 8 and so on… not ordered at all.
Here is it:
function number_column_order( $vars ) {
if ( isset( $vars['orderby'] ) && 'count' == $vars['orderby'] ) {
$vars["meta_key"] = "count";
$vars["order"] = "ASC";
$vars["orderby"] = "meta_value meta_value_num";
}
return $vars;
}
add_filter( 'request', 'number_column_order' );
If it was any other column I would have had no problem,
The thing is this column is using a total count of information that is no present on the locations posts.
This is why I can’t use a custom field from the post and use meta_value_num to order it by.
The only idea I have is making a custom field that the function will insert the information to and then to sort by it but I’m having troubles in implementing such a thing.
WordPress tutorials I’ve checked weren’t very helpful because none of them refer to my situation using information from a custom field which is not on the post itself but with relationship fields from other posts.
After a few hours messing around with the code I got it working, the only thing I can’t seem to figure out is how to get it sortable, here’s what I got so far:
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title', 'trans' ),
'number' => __( 'Number', 'trans' ),
'author' => __( 'Author', 'trans' ),
'Date' => __( 'Date', 'trans' ),
);
return $cols;
}
add_filter( "manage_edit-usa-locations_columns", "change_columns" );
function columns_content_only_locations($column_name, $post_ID) {
if ($column_name == 'number') {
$locations = get_posts(array(
'post_type' => 'post',
'category' => '3',
'numberposts'=>-1,
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'headquarters', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
$totalCount = count( $locations );
echo $totalCount;
}
}
add_action('manage_usa-locations_posts_custom_column', 'columns_content_only_locations', 10, 2);
// Make these columns sortable
function sortable_columns() {
return array(
'number' => 'number',
'title' => 'title',
);
}
add_filter( "manage_edit-usa-locations_sortable_columns", "sortable_columns" );
function number_column_order( $vars ) {
if ( isset( $vars['order'] ) && 'number' == $vars['order'] ) {
$vars = array_merge( $vars, array(
'order' => 'ASC'
) );
}
return $vars;
}
add_filter( 'request', 'number_column_order' );
I’ve tried moving the front-page.php as it is to twenty twelve and deactivated all plugins (I only use ACF & Yoast SEO which is now inactive).
I am having the same problem, when writing anything using < or > the page just refreshes and if plain text is there then it redirects to the new post url.
You can see in it here: http://html.magme.net/
What saving code? I didn’t know there was a code I needed for saving the data.
I figured it works like custom field on a template where the form is just moving the field to the front end.
What I have now is only a template for the front page with the code I posted,
Was I suppose to do something more in order for it to save?
It is working for plain text as it is tough..
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.