WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retrieving the Title and Post Content with add_filter (6 posts)

  1. goxu
    Member
    Posted 7 years ago #

    Hi,

    How do I retrieve the post title when parsing the content with add_filter('the_content', 'myfunction', 10);.

    I'm making a plugin which embeds links in the bottom of the post based on the terms used in the title and post content.

    So in this case, "myfunction" should recieve the title and the content at the same time. Is is possible to do? Do I need to make a fuction to save the title and need to call it from the myfunction?

    If you know the way, it would be greatly appreciated.

  2. Kafkaesqui

    Posted 7 years ago #

    For working with the post title you typically use the API hook 'the_title' -- but if you're testing on it to achieve an action on the post's content, you may be better off doing something like scoping the $post object to global in your function (it should be hanging around considering you're hooking into 'the_content'):

    global $post;

    From here on the post title is available through $post->post_title

  3. goxu
    Member
    Posted 7 years ago #

    Thanks, Kafkaesqui.

    I really appreciate it.

  4. goxu
    Member
    Posted 7 years ago #

    I have been working on it and found that another way of doing this.

    global $wp_query;
    $post_title = $wp_query->post->post_title;

    What is the difference? Which one is faster?

    I also wonder how the program knows which post to retrieve. I guess this only work in single pages, wp-pages. Is it correct?

  5. Kafkaesqui

    Posted 7 years ago #

    "What is the difference? Which one is faster?"

    There is really no difference as the $post object is assigned through the WP_Query class. I've used both ways.

    As far as I know there's no variation on speed between the two; they are both made 'available' on single posts and Pages.

    "I guess this only work in single pages, wp-pages. Is it correct?"

    Yes and no. They are WordPress objects classified during the post query, and there for use by both internal and external (plugin) functions. They do exist on multiple query (posts) pages, but will hold only the *latest* post on a page.

  6. goxu
    Member
    Posted 7 years ago #

    Thank you so much Kafkaesqui, for answering the questions. It helps a lot!!

Topic Closed

This topic has been closed to new replies.

About this Topic