0

Sorry if this question is repeated, i asked on stackoverflow before but then i found out this place and got a message to wait before posting again, maybe it was just a bug. I am working on a premade child theme that has a premade custom post type. I want to display some values from a few instances of this post type, like title, image, etc, however, i dont know where to find these values in order to use them in my functions.php in the wordpress dashboard. I ve been searching for them in the file manager on cpanel but its chaotic there with so many files. The theme i m using should be following a standarized structure though, so do you have any ideas where such custom post type fields are stored? Otherwise, is there any other shortcut to finding these? Like with some plugin, like Advanced Custom Fields, and if yes, how?

Also, after i have pinpointed these fields, how can i use them in my functions.php in wordpress to, lets say, display them for example, for starters? Thanks in advance, i m also a newbie to all this so try to keep it simple.

1 Answer 1

1

Hi If I understand correctly you are looking to get the data associated with a custom post type that was generated in your WordPress environment created by the theme.

This can be tricky to understand if you are not familiar with WordPress.

Typically to get any kind of data with any type of post type you can use the get_posts() function (see Wordpress docs) provided by WordPress. This typically looks like this:

$args =  array("post_type" => 'custom-post-type-name', "numberposts"=> '-1');
$post_query = get_posts($args);

What this does is retrieve all the posts with that post type and returns them in an array of WP_Post objects. (See WP_POST Docs)

What you can then do is iterate through that array and access the various items you may wish to use that are associated with that custom post type (assuming there are custom properties). Since custom post types are typically an extension of WP_POST you will have access to all the available properties of the WP_POST object. What that looks like in code:

foreach($post_query as $post){
  //The title of the post
  echo $post->post_title;
  // The content of the post
  echo $post->post_content;

  // If you custom post type has a custom property
  echo $post->custom_property_name;
  
}

This is a very basic overview of custom post types and how to access them and I would recommend looking more into them by reading more material on them. As for the code above you can simply put it into your functions.php and it should work. If you are in a development environment you could do the following to take a look at what your custom post type looks like in your functions.php file:

add_action('wp_body_open', function(){
  $args =  array("post_type" => 'custom-post-type-name', "numberposts"=> '-1');
  $post_query = get_posts($args);
  foreach($post_query as $post){
    var_dump($post);
    return;
  }
});

As far as managing custom post types goes here are some helpful articles from websites I use on the regular to get help with WordPress related projects:

WP Beginner Kinsta Resource Smashing Resource

Best of luck!

3
  • Thanks a lot for your detailed responce, however, these codes didnt show display anything to me after all, dont know if its me assuming the wrong name for custom-post-type-name or something else. Is there any shortcut to all this or should i rather start studying for whatever sources on my own? Apr 2 at 11:13
  • Honestly I am not sure why it’s not working without more info, however, the next step to figure out the post type name would be to use get _post_type() developer.wordpress.org/reference/functions/get_post_types. Take a look at the examples of code at the bottom to see how to out put more info. I would say that unfortunately there is probably no “easy” solution unless you find a plugin that would do what your need. Hopefully that helps!
    – YOUGLE
    Apr 2 at 13:35
  • i figured out what it was, it worked, it was really helpful, thank you Apr 3 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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